In my app, using MFMailComposer I'm not able to send attachments to another mail address.
Those attachments are in a link.
I'm using this code:
NSData *textData = [NSData dataWithContentsOfFile:self.fileString];
[mailView addAttachmentData:textData mimeType:#"text/plain" fileName:self.fileString];
Try below code for sending attachment in email..
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Subject"];
NSData *textData = [NSData dataWithContentsOfFile:self.fileString];
[picker addAttachmentData:imageData mimeType:#"text/plain" fileName:#"rainy"];
[self presentViewController:picker animated:YES completion:nil];
[picker release];
what you do in addAttachmentData method is replace self.fileString in fileName with other name like I have written.
let me know it is working or not.
I guess the fileName is the path of your text file. And then you pass this path ( self.fileString) to -addAttachmentData:mimeType:fileName:. It is not correct using of attach data to mail. You just need to set a plain string as the file name of the attach file, maybe #"attachText.txt" will be a good choice.
Related
[SOLVED] Guess I should have RTFM! Using [MFMailComposeViewController addAttachmentData: mimeType: fileName:] solved my problem completely. No need for base64 encoding at all :)
For anyone whose interested this question provides some good information about base64 encoding.
I allow the user to take or choose an image and attach it to an email. The email sends and delivers perfectly well in Mac Mail but on Windows (Outlook Express & gmail) the image does not display. Gmail tells me 'The Conversion Cannot Be Loaded'.
Below is the code I use to attach the image to an email. It must eb something to do with the encoding of the image. Can anyone advise?
Many thanks for any help
- (void) createEmail {
// set up the image data.
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(self.imageToUpload, 1.0)];
NSString *base64String = [imageData base64EncodedString];
NSString *emailBodyString = [NSString stringWithFormat:#"<html><body><img src='data:image/jpeg;base64,%#'></body></html>",base64String];
// create the email modal
NSArray *recipients = [[NSArray alloc] initWithObjects:#"test#email.com",nil];
MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
emailDialog.mailComposeDelegate = self;
[emailDialog setToRecipients:recipients];
[emailDialog setSubject:#"Time Sheet Submission from iPhone App"];
[emailDialog setMessageBody:emailBodyString isHTML:YES];
[self presentModalViewController:emailDialog animated:YES];
[emailDialog release];
[recipients release];
}
Guess I should have RTFM! Using [MFMailComposeViewController addAttachmentData: mimeType: fileName:] solved my problem completely. No need for base64 encoding at all :)
For anyone whose interested this question provides some good information about base64 encoding.
I have my application sending an email, but is there a way to put a photo in my email above the text i'm sending. Kind of like a header to display my logo.
// Action for submenu Email Button
- (IBAction)emailButtonPressed {
[delegate playSound:#"Click_16"];
if (connectionStatus == YES)
{
if (maxCounter)
{
NSString *filename = (NSString *)[self currentImageObject:kSerialKey AtIndex:imageCounter];
NSString* documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// set up image data for email
NSString *imageFile = [documentsDirectory stringByAppendingPathComponent:filename];
NSData *imageData = [NSData dataWithContentsOfFile:imageFile];
// set up mail view controller for message
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
if (controller != nil)
{
controller.mailComposeDelegate = self;
[controller setSubject:#"Email Subject"];
[controller setMessageBody:#"Check out this picture" isHTML:NO];
[controller addAttachmentData:imageData mimeType:#"image/png" fileName:filename];
[self presentModalViewController:controller animated:YES];
}
[controller release];
}
else
[self genericAlert:#"There are no pictures to email."];
}
else
[self genericAlert:#"You are not connected to the internet. Please connect and try again."];
}
// email delegate method to dismiss window
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self becomeFirstResponder];
[self dismissModalViewControllerAnimated:YES];
}
When you attach an image using the MFMailComposeViewController it is always displayed at the bottom of the message (under your text, but above the signature) and this cannot be changed in the current version of the framework.
It is possible, however, to encode your image data into base64 and place this directly in the HTML body of your app. I won't include the code here (you can easily google it) because this is tricky and problematic since not all readers will interpret this correctly.
If this is a header image that will be the same for all emails you can put this on a server somewhere and then include an <img> tag in your HTML email body that references this file.
If this is a dynamic image you can have your app upload it to one of the many image hosting sites, retrieve the url and again include it as the src of an <img> tag in your HTML email body.
You should use the - (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename method from the MFMailComposeViewController class.
Here's a great example of this that shows taking an image from the camera and then sending it an an email message!
In my app i am recording audio. I want to mail the recorded audio file. I am doing like this.
MFMailComposeViewController *controller = [[MFMailComposeViewControlleralloc] init];
controller.mailComposeDelegate =self ;
NSData *myData = [NSData dataWithContentsOfURL:url];
Here url is the path.
printf("\n mydata %d",[myData length]);
When i am printing the data length i am able to get the length.
[controller addAttachmentData:myData mimeType:#"audio/caf" fileName:#"name"];
[self presentModalViewController:controller animated:YES];
After doing this file is attaching but when i am downloading it in my machine(Mac or windows) i am not able to play.
can any one please help me out.
Thank You
I had the exact same problem and finally bypassed it by just adding the file extension to the filename like so:
[controller addAttachmentData:myData mimeType:#"audio/caf" fileName:#"name.caf"];
I am using MFMailComposeViewController class to send out formatted HTML email from my iPhone app. I need to include an image in the email and I added am IMG tag to my emailbody
- (IBAction)shareWithOther
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"My Message Subject"];
NSString *emailBody = #"<h3>Some and follow by an image</h3><img src=\"SG10002_1.jpg\"/>and then more text.";
[picker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:picker animated:YES];
[picker release];
}
the image file, "SG10002_1.jpg" was added to my Resource Folder, but the image didn't show up in the message body (only displayed as [?]).
Can someone please tell me what I am doing wrong like if the path of the image is wrong or is there a better way to do this?
Thanks a lot.
I strongly believe (from your question) that your image SG10002_1.jpg is located in main bundle.
If it is so, then below code should work for you. It's a complete hack from this question.
- (void)createEmail {
//Create a string with HTML formatting for the email body
NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:#"<html><body>"] retain];
//Add some text to it however you want
[emailBody appendString:#"<p>Some email body text can go here</p>"];
//Pick an image to insert
//This example would come from the main bundle, but your source can be elsewhere
UIImage *emailImage = [UIImage imageNamed:#"myImageName.png"];
//Convert the image into data
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(emailImage)];
//Create a base64 string representation of the data using NSData+Base64
NSString *base64String = [imageData base64EncodedString];
//Add the encoded string to the emailBody string
//Don't forget the "<b>" tags are required, the "<p>" tags are optional
[emailBody appendString:[NSString stringWithFormat:#"<p><b><img src='data:image/png;base64,%#'></b></p>",base64String]];
//You could repeat here with more text or images, otherwise
//close the HTML formatting
[emailBody appendString:#"</body></html>"];
NSLog(#"%#",emailBody);
//Create the mail composer window
MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
emailDialog.mailComposeDelegate = self;
[emailDialog setSubject:#"My Inline Image Document"];
[emailDialog setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:emailDialog animated:YES];
[emailDialog release];
[emailBody release];
}
You can't use images with relative paths like that in mail because that will try and look for the file from the recipients mail client.
You can either embed the image using the base64 encoded object (google html base64 image) or upload the image to a publicly accessible web server and reference the absolute URL for the image from your mail, that way the recipient's mail client can always access it.
Add it as an image/jpeg attachment. It will appear at the bottom of your message but above the signature.
There are lots of other potential ways, but they're all a bit crap.
Here is the code which was working for me,
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil)
{
// We must always check whether the current device is configured for sending emails
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
MFMailComposeViewController *composeVC = [[MFMailComposeViewController alloc] init];
composeVC.mailComposeDelegate = self;
[composeVC setSubject:#"test"];
NSString *messageBody = #"";
[composeVC setMessageBody:messageBody isHTML:NO];
UIImage *artworkImage = viewImage;
NSData *artworkJPEGRepresentation = nil;
if (artworkImage)
{
artworkJPEGRepresentation = UIImageJPEGRepresentation(artworkImage, 0.7);
}
if (artworkJPEGRepresentation)
{
[composeVC addAttachmentData:artworkJPEGRepresentation mimeType:#"image/jpeg" fileName:#"Quote.jpg"];
}
NSString *emailBody = #"Find out more App at <a href='http://itunes.apple.com/us/artist/test/id319692005' target='_self'>Test</a>";//add code
const char *urtfstring = [emailBody UTF8String];
NSData *HtmlData = [NSData dataWithBytes:urtfstring length:strlen(urtfstring)];
[composeVC addAttachmentData:HtmlData mimeType:#"text/html" fileName:#""];
//Add code
[self presentModalViewController:composeVC animated:YES];
[composeVC release];
[self dismissModalViewControllerAnimated:YES];
UIGraphicsEndImageContext();
My PDF file appears in both UIWebView and the mail on screen on iphone as an attachment. When I send it to my email (using two different email carriers) it gets stripped out-no file attachment. I can send pdfs to these email accounts on my iphone and forward them ok so I don't think its my email provider but iOS that is stripping the file out??? I can attach images ok but NOT PDF files. Not sure where the issue is and how to find where the issue is occuring. Thanks!
My code:
-(void)doSendEmail :(NSString *)use_email :(NSString *)amsg {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setToRecipients:[NSArray arrayWithObjects:use_email, nil]];
NSString *filen=self.filepath1;
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:filen ofType:#"pdf"];
NSData *pdfData = [NSData dataWithContentsOfFile:pdfPath];
[controller addAttachmentData:pdfData mimeType:#"application/pdf" fileName:self.filename1]
}
//Set messages
[controller setSubject:self.subject];
[controller setMessageBody:amsg isHTML:NO];
//Display email for user to send
[self presentModalViewController:controller animated:YES];
[controller release];
}
-(void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error {
if ( (result == MFMailComposeResultSent) && (stat==4) ) {
billsent=1;
}
[self dismissModalViewControllerAnimated:YES];
}
This is an old question but I came across it in my google searches so I thought I'd post my solution:
It is likely that your pdfPath is wrong. If the pdf is loaded as an attachment properly you should not see an icon for the attachment. Instead you will see a large image of the pdf itself.
If you set a break point right after NSData *pdfData = and hover over pdfData it will probably show you that it is nil with 0 bytes of data.