ios
ios

Reputation: 6164

Send image and text in email from within app

How can I send an image along with text, which is in the form of tabular data, in an email from within my app?

Please help and make suggestions. Thanks.

Upvotes: 2

Views: 5136

Answers (5)

Krishnabhadra
Krishnabhadra

Reputation: 34275

You can use MFMailComposeViewController from Apple to sent mail from iOS apps. Its official documentation is here. Its usage

  1. Add MessageUI.framework to your project
  2. Import necessary header files

       #import <MessageUI/MessageUI.h> 
       #import <MessageUI/MFMailComposeViewController.h>
    
  3. To sent mail, open MFMailComposerController

    if ([MFMailComposeViewController canSendMail]) { 
       MFMailComposeViewController *ctrller = [[MFMailComposeViewController alloc] init]; 
       ctrller.mailComposeDelegate = self; 
       [ctrller setSubject:@"Subject Goes Here."]; 
       [ctrller setMessageBody:@"Your message goes here." isHTML:NO]; 
       [self presentModalViewController:ctrller animated:YES]; 
       [ctrller release]; //if not using ARC
    } else { 
        NSLog(@Device is unable to send email in its current state.); 
    }
    
  4. If you want to attach data you can use addAttachmentData: method

    [ctrller addAttachmentData:YOUR_DATA_IN_NSDATA_FORMAT 
               mimeType:YOUR_MIME_TYPE 
               fileName:YOUR_ATTACHEMENT_FILENAME];
    

Upvotes: 0

visakh7
visakh7

Reputation: 26390

- (void)sendMailWithImage:(UIImage *)image
{
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
if(mailController!=nil) {
mailController.mailComposeDelegate = self;
NSData *imageData = UIImagePNGRepresentation(image);
[mailController addAttachmentData:imageData mimeType:@"image/png" fileName:@"MyImageName"];
[mailController setSubject:yourSubject];
[mailController setMessageBody:yourBody isHTML:NO];
[self presentModalViewController:mailController animated:YES];
[mailController release];
}
else
{
//Do something like show an alert
}
}

Hope this helps

Upvotes: 8

Anomie
Anomie

Reputation: 94794

You use the MFMailComposerController class to allow the user to compose and send the mail. You can attach images and other files using the addAttachmentData:mimeType:fileName: method, and the body of the message (plain text or HTML) using the setMessageBody:isHTML: method.

Note that there is no way currently to include images in the HTML using multipart/related, you would have to either use data: URIs (not supported by all clients) or images on an external server (also not supported by all clients, for privacy reasons). Or, of course, bypass Apple completely and send the mail via conversation with your own server.

Upvotes: 0

Swapna
Swapna

Reputation: 2235

You can send image as attachment, use MFMailComposerController for sending mail.

-(void)displayComposerSheet 
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [picker setSubject:@"Test Subject"];
    // Attach an image to the email
    NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",imageName] ofType:@"png"];
    NSData *myData = [NSData dataWithContentsOfFile:path];
    [picker setMessageBody:body isHTML:NO];
    if (picker != nil)  {
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }
}

Upvotes: 0

fabian789
fabian789

Reputation: 8412

Look at the MessageComposer sample App. Basically you use addAttachmentData:mimeType:fileName:.

This is from the MessageComposer app:

NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy"];

Upvotes: 1

Related Questions