0

I am sending email from iphone app it is working fine but i want that with email i should attached a pdf file which is documents folder of the app.for testing first i attached a png from resources folder of app but it does not get attached and not sent in email i am using following code.

  - (IBAction)onEmailResult

   {
if ([[MFMailComposeViewController class] canSendMail]) {
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    [picker setSubject:@"Pig Game"];


    [picker setToRecipients:toRecipients];

            int a=10;
    int b=100;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"project existing photo" ofType:@"png"];
    NSData *myData = [NSData dataWithContentsOfFile:path];
    [picker addAttachmentData:myData mimeType:@"png" fileName:@"icon.png"];

            NSString * emailBody = [NSString stringWithFormat:@"My Score %d",a];
    [picker setMessageBody:emailBody isHTML:NO];
            [self presentModalViewController:picker animated:YES];
            [picker release];
           }

else {


    int a=10;
    int b=20;
    NSString *recipients = @"mailto:imran_husain_2000@yahoo.com?&subject=Pig Game";
    NSString *body = [NSString stringWithFormat:@"&body=My Score: %d/%d, My Time: %@", a,b, time];

    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
    email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
      }
     }
Ali Imran
  • 179
  • 4
  • 12
  • why downvote i have written correct code if any thing in is now working in my side so it does not mean to downvote ? – Ali Imran May 18 '13 at 05:21
  • The MIMIE type doesn't look good. Try changing it to "image/png" . – El Tomato May 18 '13 at 05:21
  • try this two link http://stackoverflow.com/questions/2486705/storing-image-in-plist http://stackoverflow.com/questions/10846620/ios-load-image-from-plist-file – SAMIR RATHOD May 18 '13 at 05:23
  • My guess is that you got a downvote because the question you are asking can easily be answered by running a search. Of course, he or she should state the reason. – El Tomato May 18 '13 at 05:23
  • @TBlue i changed mime type from my side it is working now thansk for you comment – Ali Imran May 18 '13 at 05:25
  • @TBlue image is attached and when i download that from email it does not open it shows damaged file it shows dimensions 0*0 – Ali Imran May 18 '13 at 05:45

3 Answers3

1

Try following code snippet.

- (NSString *)pathForFile : (NSString *) fileName{
    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: fileName];
}

- (void)sendMailWithAttachedFile:(NSString *) fileName extention:(NSString *) extension{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    //    NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForResourse:fileName ofType:extension]];
    NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForFile:[NSString stringWithFormat:@"%@.%@", fileName, extension]]];
    NSData *data=[[NSData alloc]initWithContentsOfURL:outputURL];
    [picker addAttachmentData:data mimeType:@"application/pdf" fileName:@"TestOne.pdf"];
    [self presentViewController:picker animated:YES completion:nil];
}

Now Call Send Mail method As:

[self sendMailWithAttachedFile:@"TestOne" :@"pdf"];
neowinston
  • 7,584
  • 10
  • 52
  • 83
rptwsthi
  • 10,094
  • 10
  • 68
  • 109
  • kFileNameEmailReport what is this and what to give in fileName extension in your code – Ali Imran May 18 '13 at 05:30
  • i have added the my code in your edit see it does show TestOne.pdf in email but it is not attached in email – Ali Imran May 18 '13 at 05:41
  • which file i am getting this from documents folder of app and File Name is TestOne how may give that path – Ali Imran May 18 '13 at 05:53
  • failed to find PDF header: `%PDF' not found. this gives error – Ali Imran May 18 '13 at 06:00
  • // Retrieves the document directories from the iOS device NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); NSString* documentDirectory = [documentDirectories objectAtIndex:0]; NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename]; // instructs the mutable data object to write its context to a file on disk [pdfData writeToFile:documentDirectoryFilename atomically:YES]; NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); – Ali Imran May 18 '13 at 06:11
  • this is where i am saving the pdf file first – Ali Imran May 18 '13 at 06:13
  • Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter' – Ali Imran May 18 '13 at 06:16
  • Current update is final hit after it check file writing code. – rptwsthi May 18 '13 at 06:28
  • i think instead of initWithFile URL we need to get with contentsofFile – Ali Imran May 18 '13 at 06:51
  • 1
    i think i need to post another question with how to attach file from documents directory to email in iphone app – Ali Imran May 18 '13 at 06:52
0
-(void)displayMailComposerSheet
{
 NSData *soundFile = [[NSData alloc] initWithContentsOfURL:YourDocumentFile];
    [mail addAttachmentData:soundFile mimeType:@".txt" fileName:@"YourDocumentFile.txt"];
}

This code implement in displayMailComposerSheet i hope this code is useful for You

D.M Patel
  • 145
  • 1
  • 7
  • http://stackoverflow.com/questions/16620891/how-to-attach-any-document-in-email-in-iphone-app this is my question – Ali Imran May 18 '13 at 05:47
0
    MFMailComposeViewController *picker1 = [[MFMailComposeViewController alloc] init];
    NSArray *recipentsArray = [[NSArray alloc]initWithObjects:[shopDictValues objectForKey:@"vEmail"], nil];
    picker1.mailComposeDelegate = self;
    picker1.modalPresentationStyle = UIModalPresentationFormSheet;
    [picker1 setSubject:@"Subject"];
    [picker1 setToRecipients:recipentsArray];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSMutableArray *arydefault = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults] valueForKey:@"jacketCount"]];
    for (int i=0;i<arydefault.count;i++)
    {
        NSString *pdfFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@.pdf",[[arydefault objectAtIndex:i]valueForKey:@"productlinename"],[arydefault objectAtIndex:i]]];
        [picker1 addAttachmentData:[NSData dataWithContentsOfFile:pdfFilePath] mimeType:@"application/pdf" fileName:[NSString stringWithFormat:@"Order - %@",[[[NSString stringWithFormat:@"%@%i",[[arr objectAtIndex:i]valueForKey:@"productlinename"],i] componentsSeparatedByCharactersInSet: [[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@""]]];
    }
    NSString *emailBody = @"Email Body goes here.";
    [picker1 setMessageBody:emailBody isHTML:YES];
    [self presentModalViewController:picker1 animated:YES];
Milan Gupta
  • 1,181
  • 8
  • 21
Manu
  • 4,730
  • 2
  • 20
  • 45