1

I am using MFMailComposeViewController class to send out formatted HTML email from my iPhone app. I need to include an image in the email and I added am IMG tag to my emailbody

- (IBAction)shareWithOther
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    [picker setSubject:@"My Message Subject"];

    NSString *emailBody = @"<h3>Some and follow by an image</h3><img src=\"SG10002_1.jpg\"/>and then more text.";
    [picker setMessageBody:emailBody isHTML:YES];

    [self presentModalViewController:picker animated:YES];
    [picker release];
}

the image file, "SG10002_1.jpg" was added to my Resource Folder, but the image didn't show up in the message body (only displayed as [?]). Can someone please tell me what I am doing wrong like if the path of the image is wrong or is there a better way to do this?

Thanks a lot.

Snackmoore
  • 905
  • 3
  • 18
  • 32

4 Answers4

12

I strongly believe (from your question) that your image SG10002_1.jpg is located in main bundle.
If it is so, then below code should work for you. It's a complete hack from this question.

- (void)createEmail {
//Create a string with HTML formatting for the email body
    NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:@"<html><body>"] retain];
 //Add some text to it however you want
    [emailBody appendString:@"<p>Some email body text can go here</p>"];
 //Pick an image to insert
 //This example would come from the main bundle, but your source can be elsewhere
    UIImage *emailImage = [UIImage imageNamed:@"myImageName.png"];
 //Convert the image into data
    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(emailImage)];
 //Create a base64 string representation of the data using NSData+Base64
    NSString *base64String = [imageData base64EncodedString];
 //Add the encoded string to the emailBody string
 //Don't forget the "<b>" tags are required, the "<p>" tags are optional
    [emailBody appendString:[NSString stringWithFormat:@"<p><b><img src='data:image/png;base64,%@'></b></p>",base64String]];
 //You could repeat here with more text or images, otherwise
 //close the HTML formatting
    [emailBody appendString:@"</body></html>"];
    NSLog(@"%@",emailBody);

 //Create the mail composer window
    MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
    emailDialog.mailComposeDelegate = self;
    [emailDialog setSubject:@"My Inline Image Document"];
    [emailDialog setMessageBody:emailBody isHTML:YES];

    [self presentModalViewController:emailDialog animated:YES];
    [emailDialog release];
    [emailBody release];
}
Community
  • 1
  • 1
sagarkothari
  • 24,520
  • 50
  • 165
  • 235
  • I've found that the bold tags are NOT required. Maybe they used to be but unless I'm missing something my images show up without them both in the preview and the actual sent e-mail. – Stu P. Aug 13 '13 at 16:47
  • I've tried with and without the tags, however, for me the preview of the email shows the image fine. Once I click send and I open the email in my iPad's mail client, the image isn't there. If I open it in gmail it doesn't show up either. Any help would be greatly appreciated. It might be worth mentioning that I didn't find the base64EncodedString function, so I'm using base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength instead. – Rob Mar 10 '14 at 17:29
  • 1
    I got it to work. I found in another thread that I should use [imageData base64EncodedStringWithOptions:0]. No need for the tabs either. – Rob Mar 10 '14 at 17:41
3

You can't use images with relative paths like that in mail because that will try and look for the file from the recipients mail client.

You can either embed the image using the base64 encoded object (google html base64 image) or upload the image to a publicly accessible web server and reference the absolute URL for the image from your mail, that way the recipient's mail client can always access it.

nduplessis
  • 12,236
  • 2
  • 36
  • 53
  • 1
    If you mean a "data:image/jpeg,...." URI, they aren't supported by many mail clients. Sane mail clients do not open external images either. – tc. Jul 20 '10 at 12:14
  • 1
    The alternative to the methods described above is to add the image as an attachment, but form the code it doesn't look like this is what the OP wants to achieve. It looks like the image is being used as part of an HTML formatted message so adding an attachment is no good – nduplessis Jul 20 '10 at 14:17
1

Add it as an image/jpeg attachment. It will appear at the bottom of your message but above the signature.

There are lots of other potential ways, but they're all a bit crap.

tc.
  • 33,468
  • 5
  • 78
  • 96
0

Here is the code which was working for me,

Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));

        if (mailClass != nil)
        {

            // We must always check whether the current device is configured for sending emails


            UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

            UIGraphicsEndImageContext();
            MFMailComposeViewController *composeVC = [[MFMailComposeViewController alloc] init];
            composeVC.mailComposeDelegate = self;
            [composeVC setSubject:@"test"];
            NSString *messageBody = @"";
            [composeVC setMessageBody:messageBody isHTML:NO];
            UIImage *artworkImage = viewImage;
            NSData *artworkJPEGRepresentation = nil;
            if (artworkImage)
            {
                artworkJPEGRepresentation = UIImageJPEGRepresentation(artworkImage, 0.7);
            }
            if (artworkJPEGRepresentation) 
            {
                [composeVC addAttachmentData:artworkJPEGRepresentation mimeType:@"image/jpeg" fileName:@"Quote.jpg"];
            }

            NSString *emailBody = @"Find out more  App at <a href='http://itunes.apple.com/us/artist/test/id319692005' target='_self'>Test</a>";//add code
            const char *urtfstring = [emailBody UTF8String];
            NSData *HtmlData = [NSData dataWithBytes:urtfstring length:strlen(urtfstring)];
            [composeVC addAttachmentData:HtmlData mimeType:@"text/html" fileName:@""];
            //Add code
            [self presentModalViewController:composeVC animated:YES];
            [composeVC release];
            [self dismissModalViewControllerAnimated:YES];
            UIGraphicsEndImageContext();
milanjansari
  • 1,529
  • 2
  • 21
  • 33