How to Send Email with Attachments – Example Using iPhone Camera to Email a Photo
In this post: Camera Application to Take Pictures and Save Images to Photo Album, I demonstrated how you can take photos with the iPhone camera and save the captured images to the Photo Album. A reader asked if it would be possible to email the camera image in place of writing to the Photo Album, which is the focus of this tip.
Building on the previous post, the example created here starts the camera on the iPhone, and once a photo is snapped, launches the email application, attaching the resulting image to the email.
Start the iPhone Camera
The user interface of this application is quite simple, there is one button on the UI that will start the camera. Once the button is tapped, the method shown below will be called. Here we create an image picker controller, set the source type to the camera, point the delegate to self and specify not to allow image editing. From there, simply show a modal view controller to enable the camera.
- (void)buttonPressed:(UIButton *)button { // Create image picker controller UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; // Set source to the camera imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; // Delegate is self imagePicker.delegate = self; // Allow editing of image ? imagePicker.allowsImageEditing = NO; // Show image picker [self presentModalViewController:imagePicker animated:YES]; }
Convert Camera Image to NSData Object
Once the camera has taken a photo, the method below will be called, passing in a dictionary of related information, such as the original image, edited image (if any), URL to a movie (when applicable), etc. We grab the image from the dictionary and create a UIImage object. From there, dismiss the camera, then pass the camera image to the method emailImage where we will construct an email and append the image.
-- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { // Access the uncropped image from info dictionary UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; // Dismiss the camera [self dismissModalViewControllerAnimated:YES]; // Pass the image from camera to method that will email the same // A delay is needed so camera view can be dismissed [self performSelector:@selector(emailImage:) withObject:image afterDelay:1.0]; // Release picker [picker release]; }
Email Camera Image
The last step is to compose an email and attach the image from the camera. We begin be creating a MFMailComposeViewController object, setting the delegate to self. We then step through the configuration preferences for the email, setting the subject, the list of recipients (to, carbon copy, and blind carbon copy) and creating the body of the message.
Our next step is to convert theUIImage from the camera into an NSData object, which we can attach to our email. As you’ll notice in the code below, I’ve created the NSData as a PNG representation, you could also use JPG format if you prefer (which allows you to specify a value for image compression).
Once the image is attached to the email, we show a modal view controller which will launch the email application – one note, I am making the assumption that iPhone OS 3.x is the active platform.
- (void)emailImage:(UIImage *)image { MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; // Set the subject of email [picker setSubject:@"Picture from my iPhone!"]; // Add email addresses // Notice three sections: "to" "cc" and "bcc" [picker setToRecipients:[NSArray arrayWithObjects:@"emailaddress1@domainName.com", @"emailaddress2@domainName.com", nil]]; [picker setCcRecipients:[NSArray arrayWithObject:@"emailaddress3@domainName.com"]]; [picker setBccRecipients:[NSArray arrayWithObject:@"emailaddress4@domainName.com"]]; // Fill out the email body text NSString *emailBody = @"I just took this picture, check it out."; // This is not an HTML formatted email [picker setMessageBody:emailBody isHTML:NO]; // Create NSData object as PNG image data from camera image NSData *data = UIImagePNGRepresentation(image); // Attach image data to the email // 'CameraImage.png' is the file name that will be attached to the email [picker addAttachmentData:data mimeType:@"image/png" fileName:@"CameraImage"]; // Show email view [self presentModalViewController:picker animated:YES]; // Release picker [picker release]; } - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { // Called once the email is sent // Remove the email view controller [self dismissModalViewControllerAnimated:YES]; }
Caveat
To keep things simple, this application runs on a single thread. It works fine, however, there is a few second delay when moving from the camera application to the email application. Ideally you would add a little logic to better manage the user interface.
Source Code
Here is the link to the source code: Email Camera Image Application.








First let me offer my thanks for posting this for me, I mean us.. I can not even tell you how much this has helped me.. People like you are just… Thank you so much.. I want to send you a check for the work that you did on this and to express my gratitude.. I am not rich, so it will not buy you a car or a house, but I want you to have something.. How can this be done (paypal perhaps?).. You should have something for your efforts.. and generosity of time and effort… I am totally serious.. you an simply respond to my email and I will get it out to you..THANKS MAN..
Thank a lot. You saved my day. I don’t usually leave messages, but for this one I just had to. Thanks a lot!
such a gr8 work……thanx man
U hav saved my whole day……..
Great work John. Excellent.
Just wondering if it’s possible to save the image to a FTP location instead of sending it as email attachement?
Atif, you could save the file to a remote (ftp) location, however that is not a supported capability in the existing API. You would need to write the code to upload and save the file to a server.
Thanks John for your prompt reply.
I have been thinking about this for couple of hours. I’m thinking to pass NSData (Containing Image) to a .NET webservice and use C# code to save the image to disk?
My question would be can I receive NSData into a Stream object in C#?