How to Make Your iPhone App Send Email with Attachments

by mattjdrake on July 14, 2009

mailbox.jpgWould 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>
iPhoneOS3.0.sdk>System>Library>Frameworks

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];
     [picker release];
}

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

Here is how I did the whole thing on video:

http://screencast.com/t/uz7OV6XW

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”.

Learn How To Make Your Own iPhone, iPad iOS apps!

Buy Now

If you are completely new to iPhone programming and want to start to developing iPhone apps this ebook will give you everything you need to get started today.

You Get the 5 Essential Things You Need to Make iPhone Apps

  • C Procedural Programming
  • Objective-C Object Oriented Programming
  • Cocoa-Touch Model-View-Controller Design Pattern
  • Architecture of Real World iPhone Apps
  • 12 Step App Development Formula

This ebook is much different than the other books out there: it really is a *system* that includes ONLY what you need to know to make iPhone applications. Not only that, but you will get some real insight on how to make a product and how to set up your code in the best way the first time.

Plus, you will get plenty of bonuses including exclusive source code projects. For more information, head on over to the website for the ebook:

Head over to How To Make An iPhone App eBook Website website to find out how to get Matt’s eBook and the bonus source code.

Please share this if you like it!
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • FriendFeed
  • LinkedIn
  • MySpace
  • Ping.fm
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Tumblr
  • Yahoo! Bookmarks

{ 3 trackbacks }

iPhoneKicks.com
July 16, 2009 at 8:58 am
The Official Blog of Web X.0 Media
January 21, 2010 at 2:40 pm
How to Make Your iPhone app send email with attachements
April 27, 2010 at 4:03 pm

{ 3 comments }

1 peter_k_leeNo Gravatar July 14, 2009 at 4:04 pm

Yes, MFMailComposeViewController is very cool. I’ve started using it in my projects already. Also see my blog entry http://21gingerman.wordpress.com/2009/07/02/how-to-use-iphone-3-0-features-and-still-run-on-2-2/ that summarizes how to set up a project to be compiled for OS 3.0 (for the Mail Composer View) but still work on OS 2.2.x.

2 mattjdrakeNo Gravatar July 15, 2009 at 7:21 am

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.

3 jissaNo Gravatar August 4, 2009 at 2:55 am

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

Comments on this entry are closed.

Previous post:

Next post: