Attach a PDF / Doc file With my mail

I created PDF files from the Internet. Having seen the pdf file, I want to send this pdf file by e-mail using the attached pdf file. I used a lot of codes, but everything works fine for one pdf.can someone helps me.

+3
source share
3 answers

Try it,

if([MFMailComposeViewController canSendMail]){      

    MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
    mail.mailComposeDelegate=self;
    [mail setSubject:@"Email with attached pdf"];   
    NSString *newFilePath = @"get path where the pdf reside";

    NSData * pdfData = [NSData dataWithContentsOfFile:newFilePath];
[mail addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"yourpdfname.pdf"];
    NSString * body = @"";
    [mail setMessageBody:body isHTML:NO];
    [self presentModalViewController:mail animated:YES];
    [mail release];         
}
else
{
    //NSLog(@"Message cannot be sent");
}
+9
source

Thanks @Gypsa
Here is a quick code

func composeMail(){        
        if(MFMailComposeViewController.canSendMail()){

            var mail:MFMailComposeViewController = MFMailComposeViewController()
            mail.mailComposeDelegate = self

            mail.setSubject("Email with attached pdf")

            //file name "attatchment.pdf" in project bundle
            var newFilePath:NSString = NSBundle.mainBundle().pathForResource("attatchment", ofType: "pdf")!

            var pdfData:NSData = NSData(contentsOfFile: newFilePath as String)!
            mail.addAttachmentData(pdfData, mimeType: "application/pdf", fileName: "attatchment.pdf")

            var body:NSString = ""
            mail.setMessageBody(body as String, isHTML: false)
            self.presentViewController(mail, animated: true) { () -> Void in

            }

        }else{

            println("Message cannot be sent")
        }
    }

    // MARK: - MFMailComposeViewControllerDelegate
    func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!)
    {
        self.dismissViewControllerAnimated(true, completion: { () -> Void in
        })
    }
+1
source

mime type is a change for pdf, so use this mime type which works for me

NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, bounds, nil);

Then at some point in the future you will need to pass this pdfData to the MFMailComposeViewController.

MFMailComposeViewController *vc = [[[MFMailComposeViewController alloc] init] autorelease];
[vc setSubject:@"my pdf"];
[vc addAttachmentData:pdfData mimeType:@"image/pdf" fileName:@"SomeFile.pdf"];
0
source

All Articles