0

I'm using the MFMailComposerViewController to send an email from an iOS app. The mail works except when trying to add an image. The issue with the image for me is getting it using

I've seen other examples that use something like: UIImage *emailImage = [UIImage imageNamed:@"myImageName.png"]; to add the image. My image is taken from a database table using [self.photo objectForKey:kPhotoPictureKey];

     // mail
            // Email Subject
            NSString *emailTitle = @"Join. Download the iPhone app";
            // Email Content
            NSString *messageBody = @"http://www..com/";
            // To address
            NSArray *toRecipents = [NSArray arrayWithObject:@""];


            UIImageView *mailImage = [[UIImageView alloc] init];
            mailImage.image = [UIImage imageNamed:@"1.png"]; // placeholder image
            mailImage.file = [self.photo objectForKey:kPhotoPictureKey];
            [mailImage loadInBackground];

 NSString *messageBody = [NSString stringWithFormat:@"http://www.example.com/<p><b><img src='data:image/png;base64,%@'></b></p>",mailImage.image];


            MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
            mc.mailComposeDelegate = self;
            [mc setSubject:emailTitle];
            [mc setMessageBody:messageBody isHTML:NO];
            [mc setToRecipients:toRecipents];


            // Present mail view controller on screen
            [self presentViewController:mc animated:YES completion:NULL];
hanumanDev
  • 6,592
  • 11
  • 82
  • 146

3 Answers3

4

Use this mate. It works fine!!

if ([MFMailComposeViewController canSendMail]) {
            MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
            picker.mailComposeDelegate = self;
            [picker setSubject:@"SUBJECT OF THE MAIL!"];
            NSData *myData = UIImageJPEGRepresentation(IMAGE_TO_SEND, 0.9);
            [picker addAttachmentData:myData mimeType:@"image/jpg" fileName:@"IMAGE_NAME.jpg"];

            // Fill out the email body text
            NSString *emailBody = @"BODY OF THE MAIL";
            [picker setMessageBody:emailBody isHTML:NO];
            [self presentViewController:picker animated:YES completion:nil];

 }
Ushan87
  • 1,608
  • 8
  • 15
0

You have to use

- (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename
Parameters
attachment
The data to attach. Typically, this is the contents of a file that you want to include. This parameter must not be nil.
mimeType
The MIME type of the specified data. (For example, the MIME type for a JPEG image is image/jpeg.) For a list of valid MIME types, see http://www.iana.org/assignments/media-types/. This parameter must not be nil.
filename
The preferred filename to associate with the data. This is the default name applied to the file when it is transferred to its destination. Any path separator (/) characters in the filename are converted to underscore (_) characters prior to transmission. This parameter must not be nil.
Discussion
This method attaches the specified data after the message body but before the user’s signature. You may attach multiple files (using different file names) but must do so prior to displaying the mail composition interface. Do not call this method after presenting the interface to the user.

Availability
Available in iOS 3.0 and later.

Form MFMailComposeViewController Class Reference

Alex Terente
  • 12,006
  • 5
  • 51
  • 71
0

you can simply use this line to attach your image.

MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];

[mailComposer addAttachmentData:data mimeType:@"image/png" fileName:@"image.png"];
Prashant Nikam
  • 2,253
  • 4
  • 17
  • 29