I have this code, which is supposed to attach a HTML file to the email, and also display the contents of the HTML file in the body of the email. There is an image (reading.png) referenced in the HTML file that get's displayed every-other line. However, it is NOT being displayed in the body of the email. What do I have to do to get it to display?
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self; // set delegate to notify us what's happen'in
[mailer setSubject:[NSString stringWithFormat: @"Site Readings for: %@", gSiteID.globalSiteID]];
// add the image for the HTML
UIImage *toolImage = [UIImage imageNamed:@"reading.png"];
NSData *imageData = UIImagePNGRepresentation(toolImage);
[mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"reading.png"];
// attach file
NSString *file = [[NSBundle mainBundle] pathForResource:@"Surveyor" ofType:@"txt"];
NSData *surveyorTxt= [NSData dataWithContentsOfFile: file];
[mailer addAttachmentData: surveyorTxt mimeType:@"text/html" fileName: @"Surveyor.txt"];
// display the file in the body of the email
NSString *reportBody;
reportBody = [[NSString alloc] initWithData:databuffer encoding:NSASCIIStringEncoding];
[mailer setMessageBody: reportBody isHTML:true]; // indicate the body is html
[self presentModalViewController: mailer animated:TRUE];
This is the HTML which is to be displayed:
<html>
<head>
<link rel='stylesheet' type='text/css' href='default.css'/>
</head>
<body>
STA: TBM "J" Elev: 19.76<br>Desc: USGS Test Site
<div>
<table border class="boldtable">
<tr BGCOLOR="ADDAFE">
<th>STA </th>
<th>BS </th>
<th>HI </th>
<th>FS </th>
<th>ELEV </th>
<th>Desc</th>
</tr>
<p><tr>
<td> TBM "J"</td>
<td></td><td></td><td></td>
<td>19.76</td>
<td>USGS Test Site</td>
<p><tr
><td><img src="reading.png" align=center></td><td>3.14</td><td valign="middle">22.90</td>
UPDATE: I don't think I was clear enough... In the HTML which is displayed in the outgoing message, there should be an image that appears throughout the HTML. I have no problem attaching the image to the message, but it does NOT appear in the HTML, which is where I want it. So clearly attaching the image to the message is not working... (I thought it would).
HTH...
If you want to always insert attachment in email body while editing email, you can click File > Options > Mail and select Rich Text in the drop down list of Compose message in this format in Compose message section.
If images are not showing in Gmail, make sure that “Always display external images” option is checked in General Settings of Gmail. Login to your Gmail Account, click on the Gear icon located at the top right corner of your screen and then click on Settings in the drop-down menu.
I agree with the base 64 answer. Here was my solution using a common NSData base 64 category.
static NSString *EmailBody(NSString *description, UIImage *image)
{
NSString *format = @"<html>"
@"<body>"
@"<p>%@</p>"
@"<p><b><img src='data:image/png;base64,%@'></b></p>"
@"</body>"
@"</html>";
NSData *imageData = UIImagePNGRepresentation(image);
return [NSString stringWithFormat:format, description, [imageData base64EncodedString]];
}
Just to clarify things, I'll use your example.
<html>
<head>
<link rel='stylesheet' type='text/css' href='default.css'/>
</head>
<body>
STA: TBM "J" Elev: 19.76<br>Desc: USGS Test Site
<div>
<table border class="boldtable">
<tr BGCOLOR="ADDAFE">
<th>STA </th>
<th>BS </th>
<th>HI </th>
<th>FS </th>
<th>ELEV </th>
<th>Desc</th>
</tr>
<p><tr>
<td> TBM "J"</td>
<td></td><td></td><td></td>
<td>19.76</td>
<td>USGS Test Site</td>
<p><tr
><td><img src="data:image/png;base64,##########" align=center></td><td>3.14</td><td valign="middle">22.90</td>
Where ########## is replaced with the data of reading.png after it's been base 64 encoded.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With