How to embed an image in the HTML body of an email in iOS

只谈情不闲聊 提交于 2019-12-05 04:36:30
Carlos

Download the NSData+base64 category from github.

Then do the following:

NSData *imageData = [NSData dataWithContentsOfFile:pathInDocumentDirectory(imagePath)];
NSString *base64String = [imageData base64EncodedString];
NSString *imageString = [NSString stringWithFormat:@"data:image/png;base64,%@", base64String];

Finally, put the imageString in the HTML body where you want this image to appear.

Hope it helps!

crh225

From iphone email attachment

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@"Hello"];


// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"]; 
NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com",           @"third@example.com", nil]; 
NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"]; 

[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];  
[picker setBccRecipients:bccRecipients];

// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"];

// Fill out the email body text
NSString *emailBody = @"It is raining";
[picker setMessageBody:emailBody isHTML:NO];

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

To show image in gmail, you do:

MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
    mailCont.mailComposeDelegate = self; 

    NSMutableString *emailBody = [[NSMutableString alloc] initWithCapacity:20];

    NSString *linkimg = @"https://idrivethru.com/iDriveThruWeb/faces/javax.faces.resource/idrivethru_logo.png?ln=images";

    //Add the image
    [emailBody appendFormat:@"<p><a href = 'https://idrivethru.com/'> <img src='%@' align='centre' alt='iDriveThru.com'> </a></p><br/>", linkimg];

    [emailBody appendString:@"<p>This is an email with an embeded image right <b>above</b> this text</p>"];

    //NSLog(@"%@",emailBody);

    [mailCont setMessageBody:emailBody isHTML:YES];
    [self presentViewController:mailCont animated:YES completion:nil];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!