I'm writing a function to send a pdf file via email. On iOS I am encountering an issue where a pdf file gets generated and attached to an email and sent successfully, but the received email does not contain the original pdf file for some reason.
Here is my implementation using MFMailComposeViewController.
MFMailComposeViewController *mailer = [[[MFMailComposeViewController alloc] init] autorelease];
[mailer setMailComposeDelegate: id(m_delegate)];
[mailer setSubject:subject.toNSString()];
NSMutableArray *toRecipients = [[NSMutableArray alloc] init];
for(int i = 0; i < recipients.length(); i++)
{
[toRecipients addObject:recipients.at(i).toNSString()];
}
[mailer setToRecipients:toRecipients];
NSString *emailBody = body.toNSString();
[mailer setMessageBody:emailBody isHTML:NO];
// Determine the file name and extension
attachedFile = "/var/mobile/Containers/Data/Application/1DFBF012-4838-4350-B465-ECF247831EB3/Library/Application Support/test.pdf";
NSArray *filepart = [attachedFile.toNSString() componentsSeparatedByString:#"."];
NSString *filename = [filepart objectAtIndex:0];
NSString *extension = [filepart objectAtIndex:1];
// Get the resource path and read the file using NSData
NSString *filePath = [[NSBundle mainBundle] pathForResource:filename ofType:extension];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
// Determine the MIME type
NSString *mimeType;
if ([extension isEqualToString:#"jpg"]) {
mimeType = #"image/jpeg";
} else if ([extension isEqualToString:#"png"]) {
mimeType = #"image/png";
} else if ([extension isEqualToString:#"doc"]) {
mimeType = #"application/msword";
} else if ([extension isEqualToString:#"ppt"]) {
mimeType = #"application/vnd.ms-powerpoint";
} else if ([extension isEqualToString:#"html"]) {
mimeType = #"text/html";
} else if ([extension isEqualToString:#"pdf"]) {
mimeType = #"application/pdf";
}
// Add attachment
[mailer addAttachmentData:fileData mimeType:mimeType fileName:filename];
[qtController presentViewController:mailer animated:YES completion:nil];
I was able to see the file got attached to an email and sent it. I was able to receive the email sent. However, when I opened up the email it did not contain the file attached.
Any suggestion how I can troubleshoot this issue?
Here is my implementation of saving PDF file which follows standard file system path for saving application data.
/* Generate PDF file for sharing */
CGRect pageRect = CGRectMake(0, 0, 640, 480);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString * newFilePath = [documentPath stringByAppendingPathComponent:#"test.pdf"];
NSString *strFilename = newFilePath;
MyCreatePDFFile(pageRect, [strFilename UTF8String]);
I was looking for if there is a way to get the PDF file generated for trouble-shooting. It's located in Application Support directory which other application has no access to due to sand box policy. Quickest way is to enable file sharing for my app. And here is a helpful link teaches you how to enable file sharing for my app.
How to enable file sharing for my app?
So I pulled the PDF file from iPAD via iTune sharing from the app to my MAC. I was able to attached the same file and sent it via email on my MAC perfectly.
MFMailComposeViewController does not seem to complain the file but reject it silently. Is this considered a bug?
The bug was due to the attachment file path was not pointing to the pdf file wanted to be attached. Here is the fix for it:
// Get the resource path and read the file using NSData
NSString *filePath = newFilePath; // corrected the attachment file path
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
The follow implementation should run.
/* Generate PDF file for sharing */
CGRect pageRect = CGRectMake(0, 0, 640, 480);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString * newFilePath = [documentPath stringByAppendingPathComponent:#"test.pdf"];
NSString *strFilename = newFilePath;
MyCreatePDFFile(pageRect, [strFilename UTF8String]);
/* Attach PDF file generated */
NSString *emailTitle = #"Email PDF as attachment";
NSString *messageBody = #"Hey, check this out!";
NSArray *toRecipents = [NSArray arrayWithObject:#"grace#abc.com"];
//Implementation using MFMailComposeViewController
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
// Determine the file name and extension
NSArray *filepart = [strFilename componentsSeparatedByString:#"."];
NSString *filename = [filepart objectAtIndex:0];
NSString *extension = [filepart objectAtIndex:1];
// Get the resource path and read the file using NSData
NSString *filePath = newFilePath;
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
// Determine the MIME type
NSString *mimeType;
if ([extension isEqualToString:#"jpg"]) {
mimeType = #"image/jpeg";
} else if ([extension isEqualToString:#"png"]) {
mimeType = #"image/png";
} else if ([extension isEqualToString:#"doc"]) {
mimeType = #"application/msword";
} else if ([extension isEqualToString:#"ppt"]) {
mimeType = #"application/vnd.ms-powerpoint";
} else if ([extension isEqualToString:#"html"]) {
mimeType = #"text/html";
} else if ([extension isEqualToString:#"pdf"]) {
mimeType = #"application/pdf";
}
// Add attachment
[mc addAttachmentData:fileData mimeType:mimeType fileName:filename];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
Related
I am trying to attach a docx file when sending email but I am having trouble. These 2 lines are always nil. The variables filename and extension are not nil but file comes up as nil. Not sure if this matters but the docx file is dynamically generated, what if I were to use the NSData from the generated file instead of creating a new NSData variable here?
filepath = /Users/myself/components/file.docx
NSString *file = [[NSBundle mainBundle] pathForResource:filename ofType:extension];
NSData *fileData
= [NSData dataWithContentsOfFile:file];
here is the code
-(void)sendEmail:(NSString*)filePath{
if ([MFMailComposeViewController canSendMail])
{
mailComposer = [[MFMailComposeViewController alloc]init];
mailComposer.mailComposeDelegate = self;
[mailComposer setToRecipients:#[#"name#gmail.com"]];
[mailComposer setSubject:#"Mobile File Attachment"];
[mailComposer setMessageBody:#"DocX file attached" isHTML:NO];
// Determine the file name and extension
if(![filePath isEqual:nil])
{
NSArray *filepart = [filePath componentsSeparatedByString:#"."];
NSString *filename = [filepart objectAtIndex:0];
NSString *extension = [filepart objectAtIndex:1];
// Get the resource path and read the file using NSData
NSString *file = [[NSBundle mainBundle] pathForResource:filename ofType:extension];
NSData *fileData = [NSData dataWithContentsOfFile:file];
// Determine the MIME type
NSString *mimeType;
if ([extension isEqualToString:#"jpg"]) {
mimeType = #"image/jpeg";
} else if ([extension isEqualToString:#"png"]) {
mimeType = #"image/png";
} else if ([extension isEqualToString:#"doc"]) {
mimeType = #"application/msword";
} else if ([extension isEqualToString:#"docx"]) {
mimeType = #"application/msword";
} else if ([extension isEqualToString:#"ppt"]) {
mimeType = #"application/vnd.ms-powerpoint";
} else if ([extension isEqualToString:#"html"]) {
mimeType = #"text/html";
} else if ([extension isEqualToString:#"pdf"]) {
mimeType = #"application/pdf";
}
// Add attachment
[mailComposer addAttachmentData:fileData mimeType:mimeType fileName:filename];
}
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:mailComposer animated:YES completion:nil];
}
EDIT: add value of filePath and this is how I generate the file
NSAttributedString *str = [[NSAttributedString alloc] initWithAttributedString:_textV.attributedText];
//convert to html then write to a .docx file to export to docx
NSData *data = [str dataFromRange:(NSRange){0, [str length]} documentAttributes:#{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} error:NULL];
[data writeToFile:#"/Users/myself/components/file.docx" atomically:YES];
The issue here as you've mentioned is those two lines - if the file is dynamically generated, it won't be in the main bundle - that's the bundle of an application (each app on OS X and iOS is a folder /"bundle" or "package"/, which contains other files. Using this method, you are retrieving files that are packaged with the app.
If you are generating the content, you are likely writing it to a different path (temp directory?) - hence the pathForResource: method returns nil (the resource is not found within the app bundle), and NSData initializer fails when the path is nil -> returns nil as well.
You most likely want this: NSData *fileData = [NSData dataWithContentsOfFile:filePath];.
Few notes:
do not use isEqual:nil. Simply use filePath != nil (it's easier to read and it's faster on execution).
NSString has a pathExtension property, which will retrieve the property for you.
using file paths is unofficially deprecated in favor of using URLs: NSURL *fileURL = [NSURL fileURLWithPath:filePath]; You should use URLs in new code.
Please, post a sample value of filePath if this doesn't help.
Im trying to zip up my sqlite files from my documents directory to send via mail using https://github.com/mattconnolly/ZipArchive.
NSString *filePath = [[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"] stringByAppendingPathComponent:#"app.sqlite"];
NSString *filePath2 = [[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"] stringByAppendingPathComponent:#"app.sqlite-shm"];
NSString *filePath3 = [[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"] stringByAppendingPathComponent:#"app.sqlite-wal"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *archivePath = [documentsDirectory stringByAppendingString:#".zip"];
//Zip up files
ZipArchive *archiver = [[ZipArchive alloc] init];
[archiver CreateZipFile2:archivePath];
[archiver addFileToZip:filePath newname:#"backUp.sqlite"];
[archiver addFileToZip:filePath2 newname:#"backUp.sqlite-shm"];
[archiver addFileToZip:filePath3 newname:#"backUp.sqlite-wal"];
MFMailComposeViewController *mailView = [[MFMailComposeViewController alloc] init];
if (mailView != nil) {
mailView.mailComposeDelegate = self;
[mailView setSubject:#"Database"];
//Attach zip file
[mailView addAttachmentData:[NSData dataWithContentsOfFile:archivePath] mimeType:#"application/zip" fileName:#"test.zip"];
[mailView setMessageBody:#"Database attached" isHTML:NO];
[self presentViewController:mailView animated:YES completion: NULL];
}
The attachment appears in the email preview on the device but when it arrives with the recipient the attachment has vanished?
also tried this:
NSString *filePath = [[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"] stringByAppendingPathComponent:#"app.sqlite"];
NSString *filePath2 = [[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"] stringByAppendingPathComponent:#"app.sqlite-shm"];
NSString *filePath3 = [[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"] stringByAppendingPathComponent:#"app.sqlite-wal"];
DebugLog(#"DB path is: %#", filePath);
NSError *error = nil;
NSData *fileData = [[NSData alloc] initWithContentsOfFile:filePath options:0UL error:&error];
NSString* fileDataString = [[NSString alloc] initWithData:fileData encoding:NSNonLossyASCIIStringEncoding];
NSData *fileData2 = [[NSData alloc] initWithContentsOfFile:filePath2 options:0UL error:&error];
NSString* fileData2String = [[NSString alloc] initWithData:fileData2 encoding:NSNonLossyASCIIStringEncoding];
NSData *fileData3 = [[NSData alloc] initWithContentsOfFile:filePath3 options:0UL error:&error];
NSString* fileData3String = [[NSString alloc] initWithData:fileData3 encoding:NSNonLossyASCIIStringEncoding];
if (error != nil) {
DebugLog(#"Failed to read the file: %#", [error localizedDescription]);
} else {
if (fileData == nil) {
DebugLog(#"File data is nil");
}
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *archivePath = [documentsDirectory stringByAppendingString:#".zip"];
//Zip up documents directory
ZipArchive *archiver = [[ZipArchive alloc] init];
[archiver CreateZipFile2:archivePath];
[archiver addFileToZip:fileDataString newname:#"backUp.sqlite"];
[archiver addFileToZip:fileData2String newname:#"backUp.sqlite-shm"];
[archiver addFileToZip:fileData3String newname:#"backUp.sqlite-wal"];
MFMailComposeViewController *mailView = [[MFMailComposeViewController alloc] init];
if (mailView != nil) {
mailView.mailComposeDelegate = self;
[mailView setSubject:#"Database"];
//Attach zip file
[mailView addAttachmentData:[NSData dataWithContentsOfFile:archivePath] mimeType:#"application/zip" fileName:#"test.zip"];
[mailView setMessageBody:#"Database attached" isHTML:NO];
[self presentViewController:mailView animated:YES completion: NULL];
}
same behaviour zip file does not arrive with email
Logging the archivePath gives me /var/mobile/Containers/Data/Application/1D32A3AA-A431-46234-AE4B-ED944CA2D883F4/Documents.zip
Typically an email attachment doesn't arrive for one of two reasons:
The path you used to load the NSData resulted in the data being nil.
The file is too large to send via email.
You have several issues leading to problem one.
The path for the zip file is in a read-only folder - the root of your app's resource bundle.
Never use NSHomeDirectory() to create paths to folders in your app's sandbox. Things changed in iOS 8 and such paths won't work at all. Only use NSSearchPathForDirectoriesInDomains with the proper folder reference such as NSDocumentDirectory.
To solve the first issue, change this:
NSString *archivePath = [documentsDirectory stringByAppendingString:#".zip"];
to something like:
NSString *archivePath = [documentsDirectory stringByAppendingPathComponent:#"SomeFiles.zip"];
It would be even better to store this temporary file in the caches folder (use NSCachesDirectory instead of NSDocumentDirectory). Then delete the file after sending the email.
I have implemented the module for sending the email from my iPhone device to server. When it comes to the implementation, there is no email file attach instead. I sweared I have added the attACHMENT . THE below is my working
-(IBAction)sendMail:(id)sender{
NSArray *toRecipents = [NSArray arrayWithObjects:#"birth.lo#sdsdfsfsdf-hk.com",#"jason.li#asdasdad-hk.com",#"lo.sdad#gmail.com",nil];
mailComposer = [[MFMailComposeViewController alloc]init];
if ([MFMailComposeViewController canSendMail]) {
mailComposer.mailComposeDelegate = self;
[mailComposer setSubject:#"NOXAV testing"];
[mailComposer setToRecipients:toRecipents] ;
[mailComposer setMessageBody:#"Testing message for the test mail" isHTML:NO];
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:#"%#/textfile.txt",
documentsDirectory];
NSData *fileData = [NSData dataWithContentsOfFile:fileName];
NSString *mimeType = #"text/html";
[mailComposer addAttachmentData:fileData mimeType:mimeType fileName:fileName];
[self presentModalViewController:mailComposer animated:YES ];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if (result) {
NSLog(#"Result : %d",result);
}
if (error) {
NSLog(#"Error : %#",error);
}
[self dismissModalViewControllerAnimated:YES];
}
The MIME type for text file should be text/plain,So change
NSString *mimeType = #"text/html";
to
NSString *mimeType = #"text/plain";
Edit
Also change the fileName here
[mailComposer addAttachmentData:fileData mimeType:mimeType fileName:#"textfile.txt"];
Looks like file name problem, change following statement along with above suggested by Yogesh -
//make a file name to write the data to using the documents directory:
NSString * fileName = [documentsDirectory stringByAppendingPathComponent:#"textfile.txt"];
NSData *fileData = [NSData dataWithContentsOfFile:fileName];
NSString *mimeType = #"text/plain";
Your fileData may not be getting the data from file. Check if [filedata length] = some value.
I am developing native iOS application.
I have a requirement as copy an image from my application to mail app, how can i accomplish the task kindly suggest me.
Thanks in Advance..
using MFMailComposeViewController
MFMailComposeViewController *mail=[MFMailComposeViewController alloc]init];
// Determine the file name and extension
NSString *extension =#"png";
NSString * filename=#"email";
// Get the resource path and read the file using NSData
NSString *filePath = [[NSBundle mainBundle] pathForResource:filename ofType:extension];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
// Determine the MIME type
NSString *mimeType;
if ([extension isEqualToString:#"jpg"])
{
mimeType = #"image/jpeg";
}
else if ([extension isEqualToString:#"png"])
{
mimeType = #"image/png";
}
else if ([extension isEqualToString:#"doc"])
{
mimeType = #"application/msword";
}
else if ([extension isEqualToString:#"ppt"])
{
mimeType = #"application/vnd.ms-powerpoint";
}
else if ([extension isEqualToString:#"html"])
{
mimeType = #"text/html";
}
else if ([extension isEqualToString:#"pdf"])
{
mimeType = #"application/pdf";
}
// Add attachment
[mail addAttachmentData:fileData mimeType:mimeType fileName:filename];
Use MFMailComposerController and use this code:
MFMailComposeViewController * mcvc = [[MFMailComposeViewController alloc] init];
mcvc = self;
[mcvc setSubject:#"Subject"];
[mailer setToRecipients:#[#"mail#domain.com"]];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"mrweb" ofType:#"png"];
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
[mcvc addAttachmentData:imageData mimeType:#"image/png" fileName:#"allegato1.png"];
[mcvc setMessageBody:#"Hello bro !" isHTML:NO];
[self presentViewController:mcvc animated:YES completion:^{}];
If you have an UIImage use
NSData *imageData = UIImagePNGRepresentation(myImage.image);
to convert UIImage in NSData.
Here the complete list of all mime-types.
Is there any way to save a screenshot to the application's sandbox or some where that can be easily found to add to an email attachment? currently im saving it in the camera roll but can't seem to be able to get it to attach to my email
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *png = #".png";
NSString *filename = [drawquestion stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *link = [NSString stringWithFormat: #"%#%#%#", documentDirectory,filename,png];
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:link];
NSLog(link);
UIImageWriteToSavedPhotosAlbum(viewImage, drawquestion, nil, nil);
Here are 2 bare bones examples of how to attach an image to an email.
// with UIImage * image;
MFMailComposeViewController * mfcvc = [[[MFMailComposeViewController alloc] init] autorelease];
NSData * imageData = UIImagePNGRepresentation(image);
[mfcvc addAttachmentData:imageData mimeType:#"image/png" fileName:#"demo"];
[self.viewController presentModalViewController:mfcvc animated:YES];
// with UIImage * image; and float compression_quality; between 0.0 and 1.0
MFMailComposeViewController * mfcvc = [[[MFMailComposeViewController alloc] init] autorelease];
NSData * imageData = UIImageJPEGRepresentation(image, compression_quality);
[mfcvc addAttachmentData:imageData mimeType:#"image/jpeg" fileName:#"demo"];
[self.viewController presentModalViewController:mfcvc animated:YES];
To save the image in the documents directory in the app's sandbox
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *png = #".png";
NSString *filename = [drawquestion stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *imagePath = [documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#%#",filename, png]];
[imageData writeToFile:imagePath atomically:YES];
PS
I cleaned up the construction of the path, it seems your code has a path like
/var/mobile/Applications/APP_ID//var/mobile/Applications/APP_ID/Documents/filename.png
You should check out writeToFile:atomically: you get your image data, and write it to a file, this will save to the application's sandbox.
Check out this for some example code: http://blog.objectgraph.com/index.php/2010/04/05/download-an-image-and-save-it-as-png-or-jpeg-in-iphone-sdk/
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(#"%#",docDir);
NSLog(#"saving png");
NSString *pngFilePath = [NSString stringWithFormat:#"%#/test.png",docDir];
// You should swap data for your image data
NSData *data = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data writeToFile:pngFilePath atomically:YES];
You can then use the addAttachmentData:mimeType:fileName: method on MFMailComposeViewController class to attach an attachment (actually you don't even need to save the image to disk):
MFMailComposeViewController * mailVC = [[MFMailComposeViewController alloc] init];
NSData *data; // this is the data from earlier
[mailVC addAttachmentData:data mimeType:#"image/png" fileName:#"myfilename"];
[self presentModalViewController:mailVC animated:YES];
Be sure to have a correct MIME type set here. The file name you can set to what you want, it is the name of the file in the attachment as seen by the recipient.