how to email file from documents directory of iphone app - iphone

I am saving file documents directory after that i want to send that file in email but problem is that it doest not get attached i think problem is due path conflict or anything else here is the code where i save file.
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(#"documentDirectoryFileName: %#",documentDirectoryFilename);
and here is the code which i am using for sending this file in email
- (NSString *)pathForFile : (NSString *) fileName{
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: fileName];
}
- (void) sendMailWithAttachedFile : (NSString *) fileName :(NSString *) extension{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
// NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForResourse:fileName ofType:extension]];
NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForFile:[NSString stringWithFormat:#"%#.%#", fileName, extension]]];
NSData *data=[[NSData alloc]initWithContentsOfURL:outputURL];
[picker addAttachmentData:data mimeType:#"documents/pdf" fileName:#"TestOne.pdf"];
[self presentModalViewController:picker animated:YES];
}
-(IBAction)onEmailResultPDF{
[self sendMailWithAttachedFile:#"TestOne":#"pdf"];
}

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString *fileName = [[NSString alloc]initWithFormat:#"%#.pdf",giveFileName];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
NSMutableData *myPdfData = [NSMutableData dataWithContentsOfFile:pdfFileName];
[picker addAttachmentData:myPdfData mimeType:#"application/pdf" fileName:giveFileName];
[self.navigationController presentViewController:picker animated:YES completion:nil];

Related

how to send email attachement from iphone application using local file

I want to send the local file from resources folder to email but it is not working is there how to add the local file.Below is the code which sends email from documents folders of iphone but i want from resource folder
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString*giveFileName=#"CPAC_Contract_Equine";
NSString *fileName;
//fileName = [[NSString alloc]initWithFormat:#"%#.pdf",giveFileName];
fileName = [[NSString alloc]initWithFormat:#"%#",giveFileName];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
NSLog(pdfFileName);
NSMutableData *myPdfData = [NSMutableData dataWithContentsOfFile:pdfFileName];
[picker setSubject:#"CPAC Contract for Equine-Sam Veterinarian"];
[picker addAttachmentData:myPdfData mimeType:#"application/pdf" fileName:giveFileName];
[self.navigationController presentViewController:picker animated:YES completion:nil];
The code you post is almost correct, you just need to the file path the path of the file in app bundle. Replace this part of the code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
Just make it load the file from the bundle:
NSString *pdfFileName = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];

how to add cc in email while sending email from iphone app

I am sending email from iphone app it is working fine i want to add cc also in email any idea how to do this .i am using following code can you please help how to add CC in this.
thanks.
initializing controller for sending email
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString*giveFileName=#"CPAC_Contract_Equine";
NSString *fileName;
//fileName = [[NSString alloc]initWithFormat:#"%#.pdf",giveFileName];
fileName = [[NSString alloc]initWithFormat:#"%#",giveFileName];
NSString*myFileName=#"W9.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
NSMutableData *myPdfData = [NSMutableData dataWithContentsOfFile:pdfFileName];
NSString *pathJam = [[NSBundle mainBundle] pathForResource:#"W9" ofType:#"pdf"];
NSData *myDataJam = [NSData dataWithContentsOfFile: pathJam];
NSArray * toRecipients = [NSArray arrayWithObject:#""];
[picker setToRecipients:toRecipients];
[picker setSubject:#"CPAC Contract for Equine-Sam Veterinarian"];
[picker addAttachmentData:myPdfData mimeType:#"application/pdf" fileName:giveFileName];
NSString * emailBody =#"<html><body>Thank you for your participation in Consulting<br>Practitioner and Client Program.</br><br>Attached is the copy of your signed contract for your records<p>Please email or fax your completed W-9 form to<br>our PEI Support Team<br>Email:PEISupportServices#zoetis.com<br>Fax:800-741-1310<body></html>";
[picker setMessageBody:emailBody isHTML:YES];
[self.navigationController presentViewController:picker animated:YES completion:nil];
The MFMailComposeViewController has a setCcRecipients: method. You would use it like this:
[picker setCcRecipients:[NSArray arrayWithObject:#"someone#example.com"]];
NSString *subject = #"";
NSString *body = #"";
NSString *address = #"to#gmail.com";
NSString *cc = #"cc#gmail.com";
NSString *path = [NSString stringWithFormat:#"mailto:%#?cc=%#&subject=%#&body=%#", address, cc, subject, body];
NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
[mailComposeViewController setToRecipients:#[#"ibanking#com"]];
[mailComposeViewController setCcRecipients:#[#"ccs.cmc#com"]];

how to create and attach CSV file in MFMailComposer in iPhone sdk?

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

How can I convert my Zip-file to NSData to email my Zip file as an attachment

I', m using the Objective Zip library to compress several images i took. I came to the point (I guess) where I' zipping an image.
Now I'd like to send this zipped File with the mailcomposer. However I need to declare a "NSData object" within my mail function.
[picker addAttachmentData:"NSData object" mimeType:#"application/zip" fileName:#"test.zip"];
Here's a snippit of my code
-(IBAction)sendMail{
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"test.zip"];
NSArray *data = [[NSArray alloc] initWithObjects:#"first",#"second",#"third",nil];
NSString *docDir3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath4 = [NSString stringWithFormat:#"%#/foto2.jpg",paths];
[data writeToFile:pngFilePath4 atomically:YES];
NSData * fotoData = [[NSData alloc] initWithContentsOfFile:pngFilePath4];
NSFileManager *manager = [[NSFileManager alloc] init];
[manager removeItemAtPath:pngFilePath4 error:nil];
ZipFile *readFile = [[ZipFile alloc] initWithFileName:path mode:ZipFileModeCreate];
ZipWriteStream *stream = [readFile writeFileInZipWithName:#"foto2.jpg" compressionLevel:ZipCompressionLevelNone];
[stream writeData:fotoData];
[stream finishedWriting];
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate=self;
[picker addAttachmentData:"NSData Object" mimeType:#"application/zip" fileName:#"test.zip"];
Class mailclass = (NSClassFromString(#"MFMailComposeViewController"));
if([mailclass canSendMail]){
[self presentModalViewController:picker animated:YES];
}
[readFile close];
[data2 release];
[fotoData release];
}
I think i need to make another NSData object from the readFile object and place this within the [picker attachmentData: method]. Hope someone can point me in the right direction.
EDIT
Still can't get this to work properly. It takes realy long to send the zip file (Even through wifi). When I open the zip image I get an error which says that the file cannot be opened. Here's my code:
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"test.zip"];
NSArray *data = [[NSArray alloc] initWithObjects:#"first",#"second", nil];
NSString *docDir3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath3 = [NSString stringWithFormat:#"%#/foto2.jpeg",docDir3];
NSData * imageData2 = [[[NSData alloc] initWithContentsOfFile:pngFilePath3] autorelease];
[data writeToFile:pngFilePath3 atomically:YES];
ZipFile *readFile = [[ZipFile alloc] initWithFileName:path mode:ZipFileModeCreate];
ZipWriteStream *stream = [readFile writeFileInZipWithName:#"foto2.jpeg" compressionLevel:ZipCompressionLevelNone];
[stream writeData:imageData2];
[stream finishedWriting];
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate=self;
[picker addAttachmentData:[NSData dataWithContentsOfFile:path] mimeType:#"application/zip" fileName:#"test.zip"];
Class mailclass = (NSClassFromString(#"MFMailComposeViewController"));
if([mailclass canSendMail]){
[self presentModalViewController:picker animated:YES];
}
[data release];
[readFile close];
Thanks in advance for helping me out!
The ZipWriteStream is writing to path, and getting data from a file path is done using [NSData dataWithContentsOfFile:path].
[picker addAttachmentData:[NSData dataWithContentsOfFile:path]
mimeType:#"application/zip"
fileName:#"test.zip"];
Objective-Zip does throw exceptions when a write failure occurs so make sure you add a try/catch around the write operation and make sure your data is not nil.

Using ZipArchive Zip file in iphone mail attachment?

is it possible to send a zip Archive file inside an email attachment using the mail api?
Try This .. This one worked for me
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *WritableDBPath= [documentsDirectory stringByAppendingPathComponent:kFilename];
NSData *data = [NSData dataWithContentsOfFile:WritableDBPath];
[picker addAttachmentData:data mimeType:#"application/zip" fileName:#"/abc.zip"];
[picker setSubject:#"Database"];
[picker setMessageBody:#"Database testing" isHTML:NO];
[self presentModalViewController:picker animated:YES];
Yes it is possible.
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *WritableDBPath= [documentsDirectory stringByAppendingPathComponent:kFilename];
NSData *data = [NSData dataWithContentsOfMappedFile:WritableDBPath];
[picker addAttachmentData:data mimeType:#"text/richtext" fileName:#"/abc.zip"];
[picker setSubject:#"Database"];
[picker setMessageBody:#"Database testing" isHTML:YES];
[self presentModalViewController:picker animated:YES];
You can choose the filepath, filename as per your choice. Please verify the mime-type if its not work.
:)