adding new line to html body in mailcomposer - iphone

How do i add a new line characters to html body of mail composer?
I have a string:
NSString *emailBody = [NSString stringWithFormat:#"<html><b>%%0D%%0AHello,%%0D%%0AHere's a link to your product%%0D%%0Aclick here%%0D%%0A best regards</b></html>", currentProduct.url_product_details];
[picker setMessageBody:emailBody isHTML:YES];
When I set the body of a mail composer I see it without new lines.
How do i cause new lines to appear?
TIA

In HTML, newlines are <br />, not %0D%0A.
And use <p>...</p> for a paragraph.
For example,
NSString* emailBody = [NSString stringWithFormat:
#"<html><head></head><body style='font-weight:bold;'>"
#"<p>Hello,</p>"
#"<p>Here's a link to your product<br /><a href='%#'>click here</a></p>"
#"<p>Best Regards</p>"
#"</body></html>", currentProduct.url_product_details];

Related

NSString losing new lines when added to mail body

I am storing some text as an NSString. The text contains multiple paragraphs. When I log or display the text the new line characters are inserted correctly.
However, when I use the MFMailComposeViewController and add the text to the mail's body, the new lines are removed and the text runs together.
What is causing this, and how do I preserve the formatting?
I believe the way you're doing this should be working, if by line break you're referring to \n. One alternative would be to replace the occurrences of \n with <br> and set the composer's isHTML flag to YES:
NSString *emailString = [myParagraphs stringByReplacingOccurrencesOfString:#"\n" withString:#"<br>"];
[mailComposer setMessageBody:emailString isHTML:YES];
(OK, if everyone writes an answer from my comment, then I write one too...)
If you instruct the view controller to treat the text as HTML, you can preserve its formatting, else it'll be discarded. Note that in this case you'll have to feed it actual HTML, of course (\n in HTML isn't any good). So try:
NSCharacterSet *set = [NSCharacterSet newlineCharacterSet];
NSString *html = [[body componentsSeparatedByCharactersInSet:set]
componentsJoinedByString:#"<br />"];
[viewController setMessageBody:html isHTML:YES];
As mentioned by H2CO3 in comments, you need to wrap it as HTML content.
NSString *myEmailBody = #"First Line <br/> Second <br/> Third";
[composer setMessageBody:myEmailBody isHTML:YES];

MFMailComposer place Thanks,Regards text after attachment image

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.

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

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];

Emailing HTML from within an iPhone app is stopping at special characters

I have an iPhone app that will let users email some pre-determined text as HTML.
I'm having a problem in that if the text contains special characters within the text (e.g., ampersand &, >, <), the NSString variable that I use for sending the body of the email gets truncated at the special character.
I'm not sure how to fix this (I tried using the method stringByAddingPercentEscapesUsingEncoding…but this hasn't fixed the problems).
Thoughts on what I'm doing wrong / how to fix it?
Here is sample code showing what I'm trying to do
Thanks!!!
- (void)send_an_email:(id)sender {
NSString *subject_string = [NSString stringWithFormat:#"Summary of %#", commercial_name];
NSString *body_string = [NSString stringWithFormat:#"%#<br /><br />", [self.dl email_message]]; // email_message returns the body of text that should be shipped as html. If email_message contains special characters, the text truncates at the special character
NSString *full_string = [NSString stringWithFormat:#"mailto:?to=&subject=%#&body=%#", [subject_string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [body_string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[[NSURL alloc] initWithString:full_string]];
}
This works for me to send email from within the app. The way you have it, it quits the app and opens Mail. Try something like this:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
... do your email setup code
[picker setMessageBody:emailBody isHTML:YES];
Here is a good tutorial