MFMailComposer place Thanks,Regards text after attachment image - iphone

Hi I'm using MFMailComposer to send mail in my application, where i'm attaching an image and in body html content is there and finally i'm adding Thanks,Regards message to mail but all the text content including thanks,Regards is coming as one set then followed by my image attachment then sent from my iPhone signature text is coming.
I want to place thanks,regards text before sent from my iPhone signature text,how can i achieve this?

I have used NSData+Base64 by Matt Gallagher for converting image into base64 so add in your project:
Firstly create emailBody like this:
NSMutableString *emailBody = [[NSMutableString alloc] initWithString:#"<html><body>"] ;
[emailBody appendString:#"<p>Check Attachment</p>"];
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
[emailBody appendString:[NSString stringWithFormat:#"<p><b>%#</b></p>",yourString]];// yourString after image here
//close the HTML formatting
[emailBody appendString:#"</body></html>"];
Use like this
[MFMailDialog setMessageBody:emailBody isHTML:YES];
Credit goes to this answer.

Related

MFMailComposeViewController image attachment and HTML body

Hopefully simple question:
Is there a way to attach an image to the MFmailcomposeviewcontroller AND have an HTML formatted body? Everytime I try, I can do one or the other, but not both. If I set isHTML:YES, it gives me the HTML body format, but then it embeds my image attachment (not what I want). If I do isHTML:NO, the image is attached as a file (what I want) but the body message obviously won't respect my br line breaks.
Any suggestions?
-(IBAction)clickSend:(id)sender{
MFMailComposeViewController *mailComposer=[[MFMailComposeViewController alloc]init];
mailComposer.mailComposeDelegate=self;
NSString *bodyString=[self MessageBody];
[mailComposer setMessageBody:bodyString isHTML:YES];
[self presentViewController:mailComposer animated:YES completion:nil];
}
-(NSMutableString *)MessageBody{
NSMutableString *bodyString=[[NSMutableString alloc]initWithString:#"<html><body>"];
[bodyString appendString:#"<p>Sending Email with Image</p>"];
[bodyString appendString:#"<div style=\"background:url(http://t3.gstatic.com/images?q=tbn:ANd9GcQVd7uDWzEt7J4jimllpd9oTNBsQn-GFWUYIeGoiK_4-o4tPYGz); height:280px; width:510px; margin:60px 5px;\">"];
[bodyString appendString:#"</div> </body> </html>"];
return bodyString;
}
Hope, this will help you

Base64 HTML embedded images not showing when mailed

I am embedding images that have been base64 encoded in HTML as follows:
[html appendFormat:#"<html><body><p><b><img src=\"data:image/png;base64,%#\"></b></p></body><html>", base64ImageString];
I then create a new email as follows:
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
mailVC.mailComposeDelegate = self;
[mailVC setMessageBody:html isHTML:YES];
[self presentModalViewController:mailVC animated:YES];
The embedded image does show up in the new email before it is sent, but is not displayed in any email client to which the mail is delivered. I would think the fact that the image properly shows in the draft shows that the embedding process is successful, but I dont understand why it does not show when delivered. Looking at the raw HTML in the delivered mail shows: src="cid:(null)" Any help would be appreciated please!
I stumbled into this same problem and the solution was rather convoluted. It is possible to embed an image in an email. The problem is that for some weird reason the base64 encoded image must not contain new lines (super odd! I know). I'm guessing you are using the NSData+Base64 by Matt Gallagher? So was I! This category creates a multi-line base64 string. The code in the category is
- (NSString *)base64EncodedString
{
size_t outputLength;
char *outputBuffer =
NewBase64Encode([self bytes], [self length], true, &outputLength);
NSString *result =
[[NSString alloc]
initWithBytes:outputBuffer
length:outputLength
encoding:NSASCIIStringEncoding];
free(outputBuffer);
return result;
}
By replacing the third parameter of NewBase64Encode to false you will get a single line base64 string and that worked for me. I ended up creating a new function (just not to break anything else!) within the category.
- (NSString *)base64EncodedStringSingleLine
{
size_t outputLength;
char *outputBuffer =
NewBase64Encode([self bytes], [self length], false, &outputLength);
NSString *result =
[[NSString alloc]
initWithBytes:outputBuffer
length:outputLength
encoding:NSASCIIStringEncoding];
free(outputBuffer);
return result;
}
Using this function to encode the UIImage's NSData worked fine. The email clients I have tested so far all show the embedded image. Hope it works for you!
Edit: As pointed out in the comments, this solution is only partial. The image will be attached as a Data URI in the email. However, not all email clients will display the embedded image.

iPhone: attaching an image from image picker to email does not display on windows

[SOLVED] Guess I should have RTFM! Using [MFMailComposeViewController addAttachmentData: mimeType: fileName:] solved my problem completely. No need for base64 encoding at all :)
For anyone whose interested this question provides some good information about base64 encoding.
I allow the user to take or choose an image and attach it to an email. The email sends and delivers perfectly well in Mac Mail but on Windows (Outlook Express & gmail) the image does not display. Gmail tells me 'The Conversion Cannot Be Loaded'.
Below is the code I use to attach the image to an email. It must eb something to do with the encoding of the image. Can anyone advise?
Many thanks for any help
- (void) createEmail {
// set up the image data.
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(self.imageToUpload, 1.0)];
NSString *base64String = [imageData base64EncodedString];
NSString *emailBodyString = [NSString stringWithFormat:#"<html><body><img src='data:image/jpeg;base64,%#'></body></html>",base64String];
// create the email modal
NSArray *recipients = [[NSArray alloc] initWithObjects:#"test#email.com",nil];
MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
emailDialog.mailComposeDelegate = self;
[emailDialog setToRecipients:recipients];
[emailDialog setSubject:#"Time Sheet Submission from iPhone App"];
[emailDialog setMessageBody:emailBodyString isHTML:YES];
[self presentModalViewController:emailDialog animated:YES];
[emailDialog release];
[recipients release];
}
Guess I should have RTFM! Using [MFMailComposeViewController addAttachmentData: mimeType: fileName:] solved my problem completely. No need for base64 encoding at all :)
For anyone whose interested this question provides some good information about base64 encoding.

Why MFMailComposer with <img> is not showing the image in mail?

I am sending some images in mail using MFMailComposer. I am converting the image to Base64 and using <img> tag to add images to the HTML body(I am not adding it as attachment).
[htmlString appendFormat:
#"<img src='data:image/png;base64,%#' width=300 height=200 />", imageAsBase64];
The images are displaying correctly in MFMailComposer, but there are no images displayed in the actual mail which is sent from the MFMailComposer.
What should I do to make it work?
I had same problem before couple of weeks and I came to know that Gmail is not supporting embedded images. You can see images in email in other mail provider like your domain email but not in Gmail.
Try to send another email and you can see images. You need to add images as attachment then you can see images and it will display bottom of your email body.
Hope this help.
You have to add the images as an attachment. The rendered email that you see with HTML doesn't get rendered properly with the missing image URL.
here is an example: the caveat is that if you want to include things like a PDF you must include an image otherwise mfmailcomposer will fail... this in an apple bug.
I found the solution... Isubmitted a bug on Apple radar about it. MFMailcomposer has a bug in which you have to send an image along with your extra attachments in order to get the weird items like a pdf to work... try this and replace the pdf with your card:
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
NSString *emailSubject = [NSString localizedStringWithFormat:#"MedicalProfile"];
[controller setSubject:emailSubject];
NSString *fileName = [NSString stringWithFormat:#"%#.pdf", profileName];
NSString *saveDirectory = NSTemporaryDirectory();
NSString *saveFileName = fileName;
NSString *documentPath = [saveDirectory stringByAppendingPathComponent:saveFileName];
*** YOU MUST INCLUDE AN IMAGE OR THE PDF ATTATCHMENT WILL FAIL!!!***
// Attach a PDF file to the email
NSData *pdfData = [NSData dataWithContentsOfFile:documentPath];
[controller addAttachmentData:pdfData mimeType:#"application/pdf" fileName:fileName];
// Attach an image to the email
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"miniDoc" ofType:#"png"];
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
[controller addAttachmentData:imageData mimeType:#"image/png" fileName:#"doctor"];
[controller setMessageBody:[NSString stringWithFormat:#"%#'s Medical Profile attatched!", profileName] isHTML:NO];
[self presentModalViewController:controller animated:YES];
controller.mailComposeDelegate = self;
[controller release];

Sending out HTML email with IMG tag from an iPhone App using MFMailComposeViewController class

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.
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];
}
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.
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.
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();