I have made a pdf file within code, and I am sending it as an attachment to an email address. When it shows the MFMailComposeController, the attachment is there in the email, but it is not attached with the email I receive. What can be the possible reason for this? I have attached the code here.
if(
[MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailViewController=[[MFMailComposeViewController alloc]init];
[mailViewController setSubject:#"Maintenance Request Form"];
mailViewController.mailComposeDelegate=self;
[mailViewController setToRecipients:[NSArray arrayWithObject:#"BRE#appiwork.com"]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingFormat:#"/MaintenanceRequest.pdf"];
NSData *data=[NSData dataWithContentsOfFile:file];
[mailViewController addAttachmentData:data mimeType:#"application/pdf" fileName:#"MaintenanceRequest.pdf"];
[self presentViewController:mailViewController animated:YES completion:nil];
}
Related
I am trying to attach a video to an email. It is not a local video so I write it to a file. Then I get the data from that file and attach to the email. When the email comes up, the only thing there is the file name "video.mp4" with no video.
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
NSData *videoData = [NSData dataWithContentsOfURL:url];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *tempPath = [documentsDirectory stringByAppendingFormat:#"/video.mp4"];
NSLog(#"writing");
BOOL success = [videoData writeToFile:tempPath atomically:NO];
if (success){
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfFile:tempPath];
NSLog(#"path: %#", tempPath);
if (error) {
NSLog(#"%#", [error localizedDescription]);
}else{
[controller addAttachmentData:data mimeType:#"video/mp4" fileName:#"video.mp4"];
NSLog(#"Data has loaded successfully.");
}
}
I think only approach to try is formatting mail as HTML. Otherwise you will always sending an attachment.
NSString *messageBody = #" /*Some HTML format containing HTML5 video player*/";
[controller setMessageBody:messageBody isHTML:YES];
So you need to look for suitable CSS and HTML tags for your video. But don't forget to test it with most e-mail clients as you may see different looks.
I am having issues getting my program to accept the path I gave it and returning it as NSData
// Get the resource path and read the file using NSData
NSString *searchFilename = #"SafetyAuditReport.pdf"; // name of the PDF you are searching for
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory];
NSString *documentsSubpath;
while (documentsSubpath = [direnum nextObject])
{
if (![documentsSubpath.lastPathComponent isEqual:searchFilename]) {
continue;
}
NSLog(#"found %#", documentsSubpath);
}
NSData *pdfData = [NSData dataWithContentsOfFile:documentsSubpath];
// Determine the MIME type
NSString *mimeType = #"application/pdf";
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
[mc addAttachmentData:pdfData mimeType:mimeType fileName:#""];
NSLog(#"data was loaded.......");
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:nil];
When ever the program gets the the emailing section it throws a nil exception for addAttachmentData:pdfData mimeType:mimeType fileName: saying the variable I gave it was nil (pdfData) when I know the file exists in the documents directory.
Thanks in advance
plz use this code
- (IBAction)sendMailWithAttachedFile
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
// NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForResourse:fileName ofType:extension]];
//Get the file path
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"data.pdf"];
NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:path];
NSData *data=[[NSData alloc]initWithContentsOfURL:outputURL];
[picker addAttachmentData:data mimeType:#"application/pdf" fileName:#"data.pdf"];
[self presentModalViewController:picker animated:YES];
}
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self dismissViewControllerAnimated:YES completion:nil];
}
Here is how we did it.....
NSString *path = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
path = [[NSBundle mainBundle]pathForResource:#"Name of File" ofType:#"pdf"];
NSData *pdfData = [NSData dataWithContentsOfFile:path];
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.editing = YES;
mail.mailComposeDelegate = self;
[mail setSubject:#"Your subject"];
[pdfData writeToFile:path atomically:NO];
[mail addAttachmentData:pdfData mimeType:#"application/pdf" fileName:#"Name OF File.pdf"];
[self presentViewController:mail animated:YES completion:NULL];
I am trying to generate a text file and attach it to email. Previously I used this code to attach the file to email and it is done successfully.
NSString *recipient = #"myemail#me.com";
NSArray *recipients = [NSArray arrayWithObjects:recipient, nil];
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:#"Invalid Scan"];
[mailViewController setToRecipients:recipients];
[mailViewController setMessageBody:#"" isHTML:NO];
mailViewController.navigationBar.tintColor = [UIColor blackColor];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *textFilePath = [documentsDirectory stringByAppendingFormat:#"/textfile.txt"];
NSData *myData = [NSData dataWithContentsOfFile:textFilePath];
// NSLog(#"my nsdata is %#",myData);
[mailViewController addAttachmentData:myData
mimeType:#"text/csv"
fileName:textfile.txt];
But when I tried to use this statement below ,the file didn't get attached :
NSString *fileName_txtFile= #"textfile.txt";
NSString *textFilePath = [documentsDirectory stringByAppendingFormat:[NSString stringWithFormat:#"/%#",fileName_txtFile],nil];
NSData *myData = [NSData dataWithContentsOfFile:textFilePath];
[mailViewController addAttachmentData:myData
mimeType:#"text/csv"
fileName:fileName_txtFile];
I couldn't figure out, why the file is not attaching to the mail. Could you please help me in this issue?
Thanks in advance!!
I'm using the following code to attach a csv file that is generated to mail composer.
NSString *recipient = #"tejanvm92#gmail.com";
NSArray *recipients = [NSArray arrayWithObjects:recipient, nil];
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:#"CSV Export"];
[mailViewController setToRecipients:recipients];
[mailViewController setMessageBody:#"" isHTML:NO];
mailViewController.navigationBar.tintColor = [UIColor blackColor];
NSData *myData = [NSData dataWithContentsOfFile:#"yourFileName.csv"];
[mailViewController addAttachmentData:myData
mimeType:#"text/csv"
fileName:#"yourFileName"];
[self presentModalViewController:mailViewController animated:YES];
When the code runs.....the csv file is not attached to the mail properly.
But When I got the mail, it is empty. Could you please help me in finding the issue?
[NSData dataWithContentsOfFile:#"yourFileName.csv"]// its wrong way.
Please write full path of csv file
Like if your csv file in Documents directory.
then
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *csvFilePath = [documentsDirectory stringByAppendingFormat:#"/yourFileName.csv"];
after that change in your code
[NSData dataWithContentsOfFile:csvFilePath];
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.