MFMailComposeViewController image attachment and HTML body - iphone

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

Related

not able to send attachments to another email address

In my app, using MFMailComposer I'm not able to send attachments to another mail address.
Those attachments are in a link.
I'm using this code:
NSData *textData = [NSData dataWithContentsOfFile:self.fileString];
[mailView addAttachmentData:textData mimeType:#"text/plain" fileName:self.fileString];
 
Try below code for sending attachment in email..
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Subject"];
NSData *textData = [NSData dataWithContentsOfFile:self.fileString];
[picker addAttachmentData:imageData mimeType:#"text/plain" fileName:#"rainy"];
[self presentViewController:picker animated:YES completion:nil];
[picker release];
what you do in addAttachmentData method is replace self.fileString in fileName with other name like I have written.
let me know it is working or not.
I guess the fileName is the path of your text file. And then you pass this path ( self.fileString) to -addAttachmentData:mimeType:fileName:. It is not correct using of attach data to mail. You just need to set a plain string as the file name of the attach file, maybe #"attachText.txt" will be a good choice.

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

iPhone In App Email

I have my application sending an email, but is there a way to put a photo in my email above the text i'm sending. Kind of like a header to display my logo.
// Action for submenu Email Button
- (IBAction)emailButtonPressed {
[delegate playSound:#"Click_16"];
if (connectionStatus == YES)
{
if (maxCounter)
{
NSString *filename = (NSString *)[self currentImageObject:kSerialKey AtIndex:imageCounter];
NSString* documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// set up image data for email
NSString *imageFile = [documentsDirectory stringByAppendingPathComponent:filename];
NSData *imageData = [NSData dataWithContentsOfFile:imageFile];
// set up mail view controller for message
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
if (controller != nil)
{
controller.mailComposeDelegate = self;
[controller setSubject:#"Email Subject"];
[controller setMessageBody:#"Check out this picture" isHTML:NO];
[controller addAttachmentData:imageData mimeType:#"image/png" fileName:filename];
[self presentModalViewController:controller animated:YES];
}
[controller release];
}
else
[self genericAlert:#"There are no pictures to email."];
}
else
[self genericAlert:#"You are not connected to the internet. Please connect and try again."];
}
// email delegate method to dismiss window
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self becomeFirstResponder];
[self dismissModalViewControllerAnimated:YES];
}
When you attach an image using the MFMailComposeViewController it is always displayed at the bottom of the message (under your text, but above the signature) and this cannot be changed in the current version of the framework.
It is possible, however, to encode your image data into base64 and place this directly in the HTML body of your app. I won't include the code here (you can easily google it) because this is tricky and problematic since not all readers will interpret this correctly.
If this is a header image that will be the same for all emails you can put this on a server somewhere and then include an <img> tag in your HTML email body that references this file.
If this is a dynamic image you can have your app upload it to one of the many image hosting sites, retrieve the url and again include it as the src of an <img> tag in your HTML email body.
You should use the - (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename method from the MFMailComposeViewController class.
Here's a great example of this that shows taking an image from the camera and then sending it an an email message!

PDF file getting stripped from iPhone MFMailComposeViewController call

My PDF file appears in both UIWebView and the mail on screen on iphone as an attachment. When I send it to my email (using two different email carriers) it gets stripped out-no file attachment. I can send pdfs to these email accounts on my iphone and forward them ok so I don't think its my email provider but iOS that is stripping the file out??? I can attach images ok but NOT PDF files. Not sure where the issue is and how to find where the issue is occuring. Thanks!
My code:
-(void)doSendEmail :(NSString *)use_email :(NSString *)amsg {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setToRecipients:[NSArray arrayWithObjects:use_email, nil]];
NSString *filen=self.filepath1;
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:filen ofType:#"pdf"];
NSData *pdfData = [NSData dataWithContentsOfFile:pdfPath];
[controller addAttachmentData:pdfData mimeType:#"application/pdf" fileName:self.filename1]
}
//Set messages
[controller setSubject:self.subject];
[controller setMessageBody:amsg isHTML:NO];
//Display email for user to send
[self presentModalViewController:controller animated:YES];
[controller release];
}
-(void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error {
if ( (result == MFMailComposeResultSent) && (stat==4) ) {
billsent=1;
}
[self dismissModalViewControllerAnimated:YES];
}
This is an old question but I came across it in my google searches so I thought I'd post my solution:
It is likely that your pdfPath is wrong. If the pdf is loaded as an attachment properly you should not see an icon for the attachment. Instead you will see a large image of the pdf itself.
If you set a break point right after NSData *pdfData = and hover over pdfData it will probably show you that it is nil with 0 bytes of data.