How to Make Your iPhone App Send Email with Attachments

Would you like your users to be able to send email with attachments from your app?

In iPhone OS 3.0 you can easily send pictures and voice recordings from your app in an email. In fact, now your emails can be easily formated using HTML and once the user has finished the email control returns back to your app. Pretty nifty!

Here is how you do it:

Add the Framework
Add the MessageUI framework to your projects Frameworks group folder in XCode

NOTE: if you are having trouble locating the framework follow this path: Macintosh HD>Developer>Platforms>iPhoneOS.platform>developer>SDKs>

TIP: add a shortcut to the Frameworks folder to your Finder sidebar since you will need it often

Choose a Delegate
Choose one class to act as the MFMailComposeViewControllerDelegate

Import Message Framework
Import MessageUI.h and MFMailComposeViewController.h to the header file of the delegate

Make your Delegate
Indicate that your delegate class is the delegate by including this <MFMailComposeViewControllerDelegate> in the @interface declaration

Implement the Delegate Method
Implement the didFinishWithResult MFMailComposeViewControllerDelegate delegate method and make sure to return control to the program by sending the dismissModalViewControllerAnimated message.

-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult (MFMailComposeResult)result error:(NSError*)error {
     [self dismissModalViewControllerAnimated:YES];}

Start Composing the Email Use an instance of MFMailComposeViewController to start composing the email and adding the attachments. This controller gives control to the user so s/he can decide to send or cancel the message.

-(IBAction)mailIt {MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];

     picker.mailComposeDelegate = self;
     [picker setSubject:@"I have a pencil for you"];

     UIImage *roboPic = [UIImage imageNamed:@"RobotWithPencil.jpg"];
     NSData *imageData = UIImageJPEGRepresentation(roboPic, 1);
     [picker addAttachmentData:imageData mimeType:@"image/jpg" fileName:@"RobotWithPencil.jpg"];

     NSString *emailBody = @"This is a cool image of a robot I found.  Check it out!";
     [picker setMessageBody:emailBody isHTML:YES];

     [self presentModalViewController:picker animated:YES];

Now if you test this out you should be able to send an email from your app.

A Few Notes

So, as you can see it is pretty simple to compose an email in iPhone OS 3.0. You can use HTML tags to format the email; just insert the tags into your NSString that you send with the setMessageBody message. Attachments simply use the addAttachmentData method.

Note: this example assumes that you and your users are all up to date with iPhone OS 3.0. To see an example of how to check for this and accommodate your users who are not up to date go to the Apple Developer website and look over the source code for the project MailComposer.

12 thoughts on “How to Make Your iPhone App Send Email with Attachments

  1. Great post over at your blog – in my live app I tried to implement the mail in a similar way. Backwards compatibility does cloud the waters a bit though. Even so, using mail now is so much better.

  2. How to Make Your iPhone App Send Email with Attachments…

    You’ve been kicked (a good thing) – Trackback from – iPhone SDK links, community driven…

  3. nice tutorial. But i have a question :
    is there any code to open the mail box from my app? i want to be able to copy the mail body to use it in my app.
    if yes, please send me some code

  4. Good Information.
    Help me for my question.

    My one app call another app. When another app closed by home pressed. how can i go back to my first app open programmatically.

    I create one page in html5 and open it in uiwebview(browser) xcode. I open flashapp1 through url scheme (using uiapplication sharedapplication).
    n. It’s open well. but how can i return html5 page(webview). when i click home button pressed, flashapp close but not return my html page returned.
    Give me any idea?.

    HTML5 page here.

    Open Flash App1
    Open Flash App2

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>