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.
Related
I am working on a quiz app where the user's input will be stored inside a plist files. I tried researching for various ways to attach files to an email, but mostly are all for JPEG and TXT files.
I have created a plist and tried to attach the it the same way, but on the mailcomposer it only shows the filename of the attachment.
-(NSString *) dataFilePath
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:#"Answer.plist"];
}
This is the code in the composer
-(IBAction) openEmail {
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
[mailComposer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[mailComposer setToRecipients:[NSArray arrayWithObjects:#"secondary_Example#email.com", nil]];
[mailComposer setSubject:#"Subject Topic"];
[mailComposer setMessageBody:#"Message Body" isHTML:NO];
[mailComposer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Answer" ofType:#"plist"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[mailComposer addAttachmentData:myData mimeType:#"application/xml" fileName:#"Answers"];
[self presentModalViewController:mailComposer animated:YES]; }}
Am I missing something out? Please help.. Would really appreciate it.
I am trying to save a pdf from a url and then send it in an email. The sender seems to have it but the receiver does not get it.
When I do a NSLog of the NSString file I get /var/mobile/Applications/0ADE222E-6346-4C6C-8348-DA5327B980AA/Documents/myPDF.pdf
It seems like it saves but it doesn't send. Here is my code below for saving and sending
EDIT
Updated code
// This pdfURL is 0 bytes
NSData *pdfURL = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",webPage.urlString]]];
//Store the Data locally as PDF File
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *file = [documentDirectory stringByAppendingFormat:#"/myPDF.pdf"];
[pdfURL writeToFile:file atomically:YES];
//Sending the pdf
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]){
//Changed email for privacy issues
[composer setToRecipients:[NSArray arrayWithObjects:#"123#abc.com", nil]];
[composer setSubject:[NSString stringWithFormat:#"%# email",titleText]];
[composer setMessageBody:#"your custom body content" isHTML:NO];
NSLog(#"pdf %#",file);
// This pdfData is 0 bytes
NSData *pdfData = [NSData dataWithContentsOfFile:file];
[composer addAttachmentData:pdfData mimeType:#"application/pdf" fileName:#"myPDF.pdf"];
[composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:composer animated:YES];
}
If it's from a URL, store it in NSData:
NSData *pdfData = [NSData dataWithContentsOfURL[NSURL URLWithString:#"URL Of Pdf"]];
And then attach that to mail
The issue was with the webPage.urlString being empty. This code works just make sure the url string is not empty :P
i have created one csv file and i am also attaching it to MFMailComposer and it shows me to my mail composer but when i send it to user email it does not shows me attached csv file in email. i have used this code to create csv file and adding data in it.
NSMutableString *mainString=[[NSMutableString alloc]initWithString:#""];
//NSMutableArray *section = [[NSMutableArray alloc] init];
for(int i = 0;i<[NameArray count];i++)
{
NSString *string=[indexArray objectAtIndex:i];
string=[string stringByReplacingOccurrencesOfString:#"\"" withString:#"\"\""];
[mainString appendFormat:#"\"%#\"",string];
string=[NameArray objectAtIndex:i];
string=[string stringByReplacingOccurrencesOfString:#"\"" withString:#"\"\""];
[mainString appendFormat:#",\"%#\"",string];
string=[typearray objectAtIndex:i];
string=[string stringByReplacingOccurrencesOfString:#"\"" withString:#"\"\""];
[mainString appendFormat:#",\"%#\"",string];
[mainString appendFormat:#",\"%#\"",string];
[mainString appendFormat:#"\n"];
}
NSLog(#"getdatafor csv:%#",mainString);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:#"history.csv"];
// filePath = [filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData* settingsData;
settingsData = [mainString dataUsingEncoding: NSASCIIStringEncoding];
NSError *error;
[settingsData writeToFile:filePath atomically:YES];
// NSLog(#"writeok");
NSData *mediaData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMapped error:&error];
NSLog(#"Length:%d Error:%#",[mediaData length],[error localizedDescription]);
here the above code is working good i am getting [mediaData length] i am attaching the CSV file from here.
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:#"history" ofType:#"csv"];
NSData *myData = [NSData dataWithContentsOfFile:path];
// Fill out the email body text
NSString *emailBody = #"history";
[picker setMessageBody:emailBody isHTML:NO];
[picker addAttachmentData:myData mimeType:#"text/cvs" fileName:#"history"];
[self presentModalViewController:picker animated:YES];
[picker release];
the above code is also working properly. it shows me attached CSV file but when i am sending the mail by email at that time receiver is not getting the attached CSV file. what's wrong in this code.? why the receiver is not getting the attached file.?
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"CSV File"];
NSData *myData = [text dataUsingEncoding:NSUTF8StringEncoding];
[mailer addAttachmentData:myData mimeType:#"text/cvs" fileName:#"FileName"];
[self presentModalViewController:mailer animated:YES];
Where the 'text' is a string.
i have solved this issue with attaching files and other media property in MFMailComposeViewController.
NSData *data=[[arr componentsJoinedByString:#","] writeToFile:#"Bhavesh.csv" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
[mail addAttachmentData:data mimeType:#"text/csv" fileName:#"Bhavesh.csv"];
I have an app which is sending data collected via an email as an attachment. I use the following code to create and save the data to a file called 'leads.xml' in my docs folder.
NSArray *sysPath = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *sysFilePath = [sysPath objectAtIndex:0];
[picker addAttachmentData:sysFilePath mimeType:#"text/xml" fileName:#"leads.xml"];
.
.
.
[emailBody writeToFile:filePath atomically:YES];
I then try to send the saved file as an attachment using this code -
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/leads.xml", docDirectory];
When it runs I get an error, what am I doing wrong?
The mail want a NSData object, contain the files content not a path.
You will need to load the file into a NSData object like:
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [docDirectory stringByAppendingPathComponent:#"leads.xml"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
[picker addAttachmentData:fileData mimeType:#"text/xml" fileName:#"leads.xml"];
You have to supply the data, not a file path:
NSData *data = [NSData dataWithContentsOfFile:sysFilePath];
[picker addAttachmentData:data mimeType:#"text/xml" fileName:fileName];
Docs:
- (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename
attachment:
The data to attach. Typically, this is the contents of a file that you want to include. This parameter must not be nil.
Try this code
if(mailController)
{
mailController.mailComposeDelegate = self;
// NSLog(#"current index:%d",currentIndex_);
NSString *string = [NSString stringWithFormat:#"00%d",currentIndex_+43];
// NSLog(#"string:%#",string);
NSString *imageIndex = [[NSBundle mainBundle] pathForResource:string ofType:#"jpg"];
NSData *myData = [[NSData alloc] initWithContentsOfFile:imageIndex];
[mailController addAttachmentData:myData mimeType:#"application/jpg" fileName:#"Report"];
[mailController setSubject:#"your subject"];
[mailController setMessageBody:#"" isHTML:NO];
[self presentModalViewController:mailController animated:YES];
[mailController release];
}
I am sending image here instead of it you can send your text file.
This may help you
Couple of things...
You can check the file system for the simulator at:
~/Library/Application Support/iPhone Simulator/5.0/Applications/...
and see if your file is actually being saved where you expect.
Second thing:
NSString actually has a load of convenience methods for working with file paths so you don't have to check for trailing slashes and other junk. You should always use them when working with file paths.
Working with Paths
+ pathWithComponents:
– pathComponents
– completePathIntoString:caseSensitive:matchesIntoArray:filterTypes:
– fileSystemRepresentation
– getFileSystemRepresentation:maxLength:
– isAbsolutePath
– lastPathComponent
– pathExtension
– stringByAbbreviatingWithTildeInPath
– stringByAppendingPathComponent:
– stringByAppendingPathExtension:
– stringByDeletingLastPathComponent
– stringByDeletingPathExtension
– stringByExpandingTildeInPath
– stringByResolvingSymlinksInPath
– stringByStandardizingPath
– stringsByAppendingPaths:
How do I put my PDF into NSData? I have the location of the PDF as a string in my Documents Directory of the app. When I try to email it, I see the PDF in the body of the email (vs seeing an attachment icon. I don't know if that's normal or not). But when I receive the email on the other end, it does not have a PDF extension. What am I doing wrong? Thanks.
NSString *documentsDirectory = [self GetURLForPDF]; // I know this name is bad since it is really a NSString
NSLog(#"DocumentsDirectory: %#", documentsDirectory);
NSString *pdfName = [NSString stringWithFormat:#"%#/%#.pdf", documentsDirectory, title];
NSLog(#"pdfName: %#", pdfName);
NSData *pdfData = [NSData dataWithContentsOfFile:pdfName];
[mailComposer addAttachmentData:pdfData mimeType:#"application/pdf" fileName:title];
[self presentModalViewController:mailComposer animated:YES];
Based on how the pdfName variable is being set, it looks like your "title" value does not include the PDF suffix. Have you tried:
NSString *documentsDirectory = [self GetURLForPDF]; // I know this name is bad since it is really a NSString
NSLog(#"DocumentsDirectory: %#", documentsDirectory);
NSString *fullTitle = [NSString stringWithFormat:#"%#.pdf", title];
NSString *pdfName = [NSString stringWithFormat:#"%#/%#", documentsDirectory, fullTitle];
NSLog(#"pdfName: %#", pdfName);
NSData *pdfData = [NSData dataWithContentsOfFile:pdfName];
[mailComposer addAttachmentData:pdfData mimeType:#"application/pdf" fileName:fullTitle];
[self presentModalViewController:mailComposer animated:YES];