how to attatch a file to the mail using default mail composer - ipad

i hav a file with some data and i want to attach this file to mail, and i am using the default mail composer ...
any idea how to attach the file to mail before sending to receipents....
Thanks in advance....

Example with an attached image:
NSData *myData = UIImagePNGRepresentation(myImage);
[mailComposer addAttachmentData:myData mimeType:#"image/png" fileName:#"MyImage"];

NSString *path = [[NSBundle mainBundle] pathForResource:#"bg" ofType:#"jpg"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[controller addAttachmentData:myData mimeType:#"image/jpg" fileName:#"Attachment-1"];
[self presentModalViewController:controller animated:YES];

Related

Send UImages in email as pdf

I have 10 images that are collected and imported into email and the user will send the images but they keep showing up as attachments and I need the images to be one behind the other and not as attachments. I was thinking of a pdf but I don't know how to generate one...
Here is my code:
- (IBAction)openMail:(id)sender
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"Contents Concepts Photo report"];
[mailer.view setNeedsDisplay];
NSString *path = [[NSBundle mainBundle] pathForResource:#"ContentsConceptsLogoFinal6copy" ofType:#"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[mailer addAttachmentData:myData mimeType:#"image/png" fileName:#"photo name"];
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"1" ofType:#"png"];
NSData *myData1 = [NSData dataWithContentsOfFile:path1];
[mailer addAttachmentData:myData1 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageData = UIImagePNGRepresentation(self.personimgThumbNail.image);
[mailer addAttachmentData:imageData mimeType:#"image/png" fileName:#"Imageone"];
NSString *path2 = [[NSBundle mainBundle] pathForResource:#"2" ofType:#"png"];
NSData *myData2 = [NSData dataWithContentsOfFile:path2];
[mailer addAttachmentData:myData2 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageDatatwo = UIImagePNGRepresentation(self.personimgThumbNailtwo.image);
[mailer addAttachmentData:imageDatatwo mimeType:#"image/png" fileName:#"Imagetwo"];
NSString *path3 = [[NSBundle mainBundle] pathForResource:#"3" ofType:#"png"];
NSData *myData3 = [NSData dataWithContentsOfFile:path3];
[mailer addAttachmentData:myData3 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageDatathree = UIImagePNGRepresentation(self.personimgThumbNailthree.image);
[mailer addAttachmentData:imageDatathree mimeType:#"image/png" fileName:#"Imagethree"];
NSString *path4 = [[NSBundle mainBundle] pathForResource:#"4" ofType:#"png"];
NSData *myData4 = [NSData dataWithContentsOfFile:path4];
[mailer addAttachmentData:myData4 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageDatafour = UIImagePNGRepresentation(self.personimgThumbNailfour.image);
[mailer addAttachmentData:imageDatafour mimeType:#"image/png" fileName:#"Imagefour"];
NSString *path5 = [[NSBundle mainBundle] pathForResource:#"5" ofType:#"png"];
NSData *myData5 = [NSData dataWithContentsOfFile:path5];
[mailer addAttachmentData:myData5 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageDatafive = UIImagePNGRepresentation(self.personimgThumbNailfive.image);
[mailer addAttachmentData:imageDatafive mimeType:#"image/png" fileName:#"Imagefive"];
NSString *path6 = [[NSBundle mainBundle] pathForResource:#"6" ofType:#"png"];
NSData *myData6 = [NSData dataWithContentsOfFile:path6];
[mailer addAttachmentData:myData6 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageDatasix = UIImagePNGRepresentation(self.personimgThumbNailsix.image);
[mailer addAttachmentData:imageDatasix mimeType:#"image/png" fileName:#"Imagesix"];
NSString *path7 = [[NSBundle mainBundle] pathForResource:#"7" ofType:#"png"];
NSData *myData7 = [NSData dataWithContentsOfFile:path7];
[mailer addAttachmentData:myData7 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageDataseven = UIImagePNGRepresentation(self.personimgThumbNailseven.image);
[mailer addAttachmentData:imageDataseven mimeType:#"image/png" fileName:#"Imageseven"];
NSString *path8 = [[NSBundle mainBundle] pathForResource:#"8" ofType:#"png"];
NSData *myData8 = [NSData dataWithContentsOfFile:path8];
[mailer addAttachmentData:myData8 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageDataeight = UIImagePNGRepresentation(self.personimgThumbNaileight.image);
[mailer addAttachmentData:imageDataeight mimeType:#"image/png" fileName:#"Imageeight"];
NSString *path9 = [[NSBundle mainBundle] pathForResource:#"9" ofType:#"png"];
NSData *myData9 = [NSData dataWithContentsOfFile:path9];
[mailer addAttachmentData:myData9 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageDatanine = UIImagePNGRepresentation(self.personimgThumbNailnine.image);
[mailer addAttachmentData:imageDatanine mimeType:#"image/png" fileName:#"Imagenine"];
NSString *path10 = [[NSBundle mainBundle] pathForResource:#"10" ofType:#"png"];
NSData *myData10 = [NSData dataWithContentsOfFile:path10];
[mailer addAttachmentData:myData10 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageDataten = UIImagePNGRepresentation(self.personimgThumbNailten.image);
[mailer addAttachmentData:imageDataten mimeType:#"image/png" fileName:#"Imageten"];
[self presentModalViewController:mailer animated:YES];
}
}
The only explanation I can come up with is if you compile the images into one PDF, and attach the PDF. More on that here-
-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName: (NSString*)aFilename
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[aView.layer renderInContext:UIGraphicsGetCurrentContext()];
// remove PDF rendering context
UIGraphicsEndPDFContext();
// 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);
}

Unable to attatch a csv file to mail in objective c

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

How to remove the path from an attachment when calling addAttachmentData?

When adding an attachment to an email, the file name gets it's complete path.
For a file located in:
/var/mobile/Applications/C3BBAA5F-07FE-4E26-9661-CB492E06BD2E/Documents/
I'm getting as a result, a file named:
_var_mobile_Applications_C3BBAA5F-07FE-4E26-9661-CB492E06BD2E_Documents_Clock.sqlite
When I need my filename to be:
Clock.sqlite
Here's my code:
NSString *path = [self getDatabaseFilePath];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"application/x-sqlite3" fileName:path];
How can I get the attachment to have only the filename and extension, without the complete path?
Thank you!
This should do what you want:
NSString *path = [self getDatabaseFilePath];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"application/x-sqlite3" fileName:[path lastPathComponent]];

Centering Picture In MFMailComposeViewController (iOS)

Is it possible to center an image when attaching it to the MFMailComposeViewController in XCode? Here's the code I'm using:
NSString *path = [[NSBundle mainBundle] pathForResource:#"millersicon" ofType:#"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"image/png" fileName:#"millersicon"];
It places the image in the e-mail when the Mail Application comes up but it's left aligned. Is there a way to center it? I couldn't find any information on it. Thanks.

How to add gif to attachment in iPhone mail?

I add .jpg this way:
UIImage *sentPic = self.image;
NSData *imageData = UIImageJPEGRepresentation(sentPic, 1);
[picker addAttachmentData:imageData mimeType:#"image/jpg" fileName:#"pic.jpg"];
I change it to "image/gif".But it doesn't work fine. Th Gif is just a still image in the mail.
NSData *imageData = [[NSData alloc] initWithContentsOfFile:pathToGifFile];
[picker addAttachmentData:imageData mimeType:#"image/gif" fileName:#"pic.gif"];
[imageData release];
This would add it as attachment, but I'm not sure if that's what you want, or you want to display the image inside the message?
Have you ever tried HTML code in your email like this:
NSString *emailBody = #"It is a gif email!";
emailBody = [EmailBody stringByAppendingString:#"<img src='http://xx.xx.pic.gif'>"];
[picker setMessageBody:emailBody isHTML:YES];
The code I haven't tested, you can have a try. Tell us if it works.
I just tested this. Works great:
NSString *pathForGif = [[NSBundle mainBundle] pathForResource: #"<your file name>" ofType: #"gif"];
NSData *gifData = [NSData dataWithContentsOfFile: pathForGif];
[mailController addAttachmentData:gifData mimeType:#"image/gif" fileName:#"EmailGIF.gif"];
[self presentModalViewController:mailController animated:YES];

Resources