Send HTML email with an IMG tag from an iPhone application using the MFMailComposeViewController class

I am using the MFMailComposeViewController class to send formatted HTML emails from an iPhone application. I need to include an image in an email, and I added an IMG tag to my email address

- (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 did not appear in the message body (only displayed as [?]). Can someone please tell me what I am doing wrong if the image path is wrong or is there a better way to do this?

Many thanks.

+1
source share
4 answers

( ), SG10002_1.jpg .
, . .

- (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];
}
+12

, , .

base64 ( google html base64) - URL- , , - .

+3

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

There are many other possible ways, but they are all a bit of shit.

+1
source

Here is the code that worked 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();
0
source

All Articles