How to Make Your iPhone App Send Email with Attachments

Matthew Campbell, July 14th

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!

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.

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.

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!

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

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.

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.

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.

How to Make Your iPhone App Send Email with Attachments…

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

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

[...] http://howtomakeiphoneapps.com/2009/07/how-to-make-your-iphone-app-send-email-with-attachments/ Categories: iPhone Dev Assistance Tags: coding [...]

[...] http://howtomakeiphoneapps.com/2009/07/how-to-make-your-iphone-app-send-email-with-attachments/ [...]

  • Hours of videos, demos & screen casts
  • SOURCE CODE
  • Detailed HANDS-ON Exercises
  • Much Much Much More
  • UIKit
  • Objective-C
  • Core Data
  • More!
  • Learn How To Make An iPhone App right now using my proven system.

    PS: Tons of us are having a blast living off our own code, join us now. Click here to get all the details.