iPhone MessageUI email attachments not being sent to receiver - iphone

I am trying to attach an image and a pdf to an email using the MessageUI framework. I followed the MailComposer example in the Apple documentation.
On the iPhone it seems to work perfectly, the image and the pdf both show up in the body of the send mail window as expected.
However, when I receive the email on my MacBook there are two problems.
1) myImage.png shows up as an attachment and is the correct dimensions but is totally blank
2) myPDF.pdf doesn't show up as an attachment at all
But, when I receive the mail on my iPhone, myImage.png shows up fine. myPDF.pdf still doesn't show up in mail on my iPhone though.
Hope someone can shed some light on what might be happening.
Here is the relevant code:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Test Email"];
// Attach an image to the email
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"myImage" ofType:#"png"];
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
[picker addAttachmentData:imageData mimeType:#"image/png" fileName:#"myImage"];
// Attach a PDF file to the email
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:#"myPDF" ofType:#"pdf"];
NSData *pdfData = [NSData dataWithContentsOfFile:pdfPath];
[picker addAttachmentData:pdfData mimeType:#"application/pdf" fileName:#"myPDF"];
// Fill out the email body text
NSString *emailBody = #"This is a test.";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
EDIT Instead of saving and retrieving my image and PDF to my mainBundle I used NSDocumentsDirectory and everything worked fine.

I had the same problem regarding the image attachment. Using this, I was able to send and actually receive the attached image:
//attachment
UIImage *image = [UIImage imageNamed:#"image.png"];
NSData *imageData = UIImagePNGRepresentation(image);
[picker addAttachmentData:imageData mimeType:#"image/png" fileName:#"theImage"];
I didn't try pdf sending, sorry.
Just my two cents,
Regards.

I don't know if it actually would cause your problem, but most people seem to include filename extensions on the fileName parameter to addAttachmentData, i.e.
[picker addAttachmentData:imageData mimeType:#"image/png" fileName:#"myImage.png"];

Try This ,This one worked for me .
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Test Email"];
// Attach an image to the email
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"myImage" ofType:#"png"];
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
[picker addAttachmentData:imageData mimeType:#"image/png" fileName:#"myImage"];
// Attach a PDF file to the email
//NSString *pdfPath = [[NSBundle mainBundle] pathForResource:#"myPDF" ofType:#"pdf"];
//NSData *pdfData = [NSData dataWithContentsOfFile:pdfPath];
[picker addAttachmentData:pdfData mimeType:#"application/pdf" fileName:#"myPDF.pdf"];
// Fill out the email body text
NSString *emailBody = #"This is a test.";
[picker setMessageBody:emailBody isHTML:NO];
// [self presentModalViewController:picker animated:YES];
[picker release];

I have had the same problem and I determined that the MFMailComposeViewController is packaging the MIME for the PDF file with 7-bit transfer encoding, rather than quoted-printable or base-64 encoding. Obviously you cannot email binary files using 7-bit encoding. So either there is a transfer-encoding setting on a MFMailComposeViewController that I am missing or this object has a serious shortcoming.

I came across this as I was having the same issue...little old but in case anyone comes across this here is what helped me....
I was able to add my attachments in as usual (using 6 attachments of various types)...they would show up in the email body...but the receiver would never get all of the attachments....only one.
I pulled the files into my editor (drag and drop into your m or h files) and it showed the file path. Turns out that I had neglected to copy the first file to my project so the app was pulling from my local drive and then stopping as the other files I was trying to attach were not in the same folder. Once I pulled all of the attachments from the same location they all pulled in fine.
Hope that helps someone.

Related

Not Able to Attach MSWord Documents in MFMailComposer

In MFMailComposer I'm attaching MSWord documents. Those documents are attached to MFMailComposer. I sent those documents to another mail address. In the destination mail those attachments are displayed with a download option and not with a view option.
i'm using this code
NSURL *url = [[NSURL alloc] initWithString:self.fileString];
NSData *attachments = [NSData dataWithContentsOfURL:url];
[mailView addAttachmentData:attachments mimeType:#"application/pdf/text/msword/csv" fileName:self.useridString];
You have to set mime type for MSWORD documents..
The "application/msword" MIME type is the type for the word 2003 ".doc" files. The correct MIME type for word 2007 ".docx" files is:
application/vnd.openxmlformats-officedocument.wordprocessingml.document
and for more msword mimetypes, use these links link1 and link2
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Your Subject"];
[picker addAttachmentData:yourData mimeType:#"application/csv" fileName:#"fileName"];
[picker addAttachmentData:yourData mimeType:#"application/vnd.openxmlformats-officedocument.wordprocessingml.document" fileName:#"fileName"];
}
Edited:
Check the url contains which formate. I mean this url contain .pdf and get that pdf. and so on...
NSArray *arrayComp = [self.fileString componentsSeparatedByString:#"/"];
NSString *fileType = [[[arrayComp lastObject] componentsSeparatedByString:#"."] lastObject];
NSString *mimeType = [NSString stringWithFormat:#"application/%#",fileType];
NSURL *url = [[NSURL alloc] initWithString:self.fileString];
NSData *attachments = [NSData dataWithContentsOfURL:url];
[picker addAttachmentData:attachments mimeType:mimeType fileName:#"fileName"];
I think it will be helpful to you.

Send Mail via iOS

i want to send a Mail in my app with an .PHP file as Attachment:
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:#""];
[mailViewController setMessageBody:#"" isHTML:NO];
mailViewController.navigationBar.tintColor = [UIColor blackColor];
NSData *myData = [NSData dataWithContentsOfFile:[self getScriptPath]];
NSString *fileName = #"upload.php";
[mailViewController addAttachmentData:myData mimeType:#"text/html" fileName:fileName];
[self presentViewController:mailViewController animated:YES completion:nil];
[mailViewController release];
}
else
NSLog(#"Device is unable to send email in its current state.");
The Mail is being sent, but without the attachment!...
I bet your [self getScriptPath] method return an invalid path, or a path to a non-existing file, and thus [NSData dataWithContentsOfFile:...] probably return nil.
You should use a breakpoint or NSLog to check the value returned by getScriptPath and fix it accordingly.
For example if you embedded the "upload.php" file in your application (you added it in your Xcode project, along with the other resources of your application), you should use this to get the valid path of your script file in your bundle:
-(NSString*)getScriptPath
{
return [[NSBundle mainBundle] pathForResource:#"upload" ofType:#"php"];
}
See the Bundle Programming Guide documentation for more info.
Use this code to attach image file.
UIImage *emailImage = [UIImage imageNamed:#"loveCoding.png"];
[mailViewController addAttachmentData:UIImageJPEGRepresentation(emailImage, 1) mimeType:#"image/png" fileName:#"loveCocoa.png"];
To attach file
NSString *fileName = [NSString stringWithFormat:#"Documents/%#.txt",sGame.userName];
NSString *fullFilePath = [NSHomeDirectory() stringByAppendingPathComponent:fileName];
NSString *emailFileName = [NSString stringWithFormat:#"%#_Report.txt",sGame.userName];
NSData *myData = [NSData dataWithContentsOfFile:fullFilePath];
[email addAttachmentData:myData mimeType:#"text/plain" fileName:emailFileName];
Looks like your NSData is not loading correctly. Check your path from getScriptPath

Can't find vcard attachment with the mail

I am trying to send an email from my iphone, with a vcard as attachment. When I am sending the mail, the vcard is being attached with the mail. But the receiver of the mail can't find the vcard attachment. Help needed.
This is the code I have used
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString *path = [[NSBundle mainBundle] pathForResource:#"Vcard" ofType:#"vcf"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"text/x-vcard" fileName:#"Vcard.vcf"];
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
Thanks
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 SDK Add Image to the Body of an Email?

I was wondering if anyone knew how to use an image in the body of an email when the mail app is opened. Can anyone help?
Thanks in advance Kieran.
NSData *photoData = UIImageJPEGRepresentation([UIImage imageNamed:#"qwerty.png"], 1);
MFMailComposeViewController *viewController = [[MFMailComposeViewController alloc] init];
[viewController addAttachmentData:photoData mimeType:#"image/jpg" fileName:[NSString stringWithFormat:#"photo.png"]];
I wrote the code by hand so it might have some mistake, but you should get the general idea.
you can attach image in mail body using base64 encoding
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
NSData *imageData = UIImageJPEGRepresentation(image, 0.8);
NSString *imageStr = [imageData base64EncodingWithLineLength:[imageData length]];
NSString *htmlStr = [NSString stringWithFormat:#".....<img src='data:image/jpeg;base64,%#'>.......",imageStr];
[mailComposer setMessageBody:htmlStr isHTML:YES];
[self presentModalViewController:mailComposer animated:YES];
[mailComposer release];

Can't sent csv file attachment with mail

I have used the MFMailComposeViewController to send the generated report(csv).
Now mail is sent to To:email id, & but when i checked the mails i did received the mail but attachment was not there.
Then I also tried MailComposer example :
https://developer.apple.com/iphone/library/samplecode/MailComposer/index.html
in which the png image is attached to mail demo. I also sent mail using that app, But same result image attachment is not delivered.
Help, to find what's the problem?
Thanks in advance.
Here is code in that app :
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Hello from California!"];
// 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 in sunny California!";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
In my situation the email was sent fine from my iPhone, but there was no attachment when sending from my wife's iPhone. Turns out that her default account was set to her Yahoo account and that was not allowing attachments. I simply switched it to her mobile Me account and the attachment was made. I use gmail so I never had a problem.
in addition, I had tried switching the MIME type from text/csv to text/plain and that did not help. It worked either way on my iPhone and not at all on my wife's.
Hope this helps!
I have tried to attach .csv file with "text/plain" & I got success. I think you should try the same. best of luck.
I ran into the same problems and I fixed the problem by
doing a really hard look at two functions that I wrote:
One defines the filename from a .plist file, the other one actually sets the
location of the file (including it's path AND filename).
Using the right function to retrieve the filename in the MFMailViewController
fixed the issue:
-(NSString *)dataFilePath { //this gets the ,plist file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
//----------------------------
-(NSString *)fileName { // this defines the fileName from the values in the .plist
NSString *patientDetails = [self dataFilePath];
NSArray *dataArray = [[NSArray alloc] initWithContentsOfFile:patientDetails];
NSString *firstName = [dataArray objectAtIndex:0];
NSString *lastName = [dataArray objectAtIndex:1];
NSString *fileName = [firstName stringByAppendingString:lastName];
return [fileName stringByAppendingString:#".csv"];
//-------------------------
-(NSString *)setFileLocation { //this puts name and folder together.
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *fileName = [self fileName];
NSString *fullFilePath = [NSString stringWithFormat:#"%#/%#", docDirectory,fileName];
// NSLog(#"Filepath is : %#",fullFilePath);
return fullFilePath;
}
//-------------------------
The last one is the function you want to call in your MFMailComposeViewController:
...
NSData *csvData = [NSData dataWithContentsOfFile:[self setFileLocation]];
[picker addAttachmentData:csvData mimeType:#"text/csv" fileName:fileName];
...
fileName, of course, is an NSString, retrieved by calling the fileName function:
NSString *fileName = [self fileName];
Hope this helps!
If your code looks okay I'll suspect that that myData didn't get loaded with data is nil. Put in an NSLog([myData description]) or use the debugger to check that myData is not nil.
I'm out of ideas. If I was stuck in this situation I would create a stand alone project with the example code you provided and see if I could get it working. Usually in these situations the code you are focusing on is probably correct and error is coming from somewhere else.
Also:
Check to see if the path variable is nil because that will cause the attachment to not appear.
Make sure there is a icon in message body the mail compose view. When you successfully add an attachment you should see the icon representation in the view.
And you probably want set your fileName to rainy.png, i.e. [picker addAttachmentData:myData mimeType:#"image/png" fileName:#"rainy.png"].
Good luck.