I have an HTML embedded in my app, and I want to be able to email it within the app. I have the code below set up, and it will email the text from the Email, but not attach the images embedded with it. I believe this is due to not having the baseURL set up properly but I'm not sure how to do that for the emailBody. Any thoughts?
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"Thanks" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"A prayer gift of thanks to you"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:#"friend#example.com"];
[picker setToRecipients:toRecipients];
// Fill out the email body text
NSString *emailBody = htmlString;
[picker setMessageBody:emailBody isHTML:YES];
NSString *emailBody = #"<html><body>";
emailBody = [emailBody stringByAppendingString:#"<img src='http://icons.iconarchive.com/icons/arrioch/halloween/512/devil-icon.png' width = '64' height='64'/>"];
emailBody = [emailBody stringByAppendingString:#"<br>"];
emailBody = [emailBody stringByAppendingFormat:#"%#",NSLocalizedString(#"My text", #"")];
emailBody = [emailBody stringByAppendingString:#"<br>"];
emailBody = [emailBody stringByAppendingString:#"</body></html>"];
[picker setMessageBody:emailBody isHTML:YES];
Related
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 a pdf file called FlashCards.pdf
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
NSArray *recipients = #[#"aarone_2010#hotmail.com"];
// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:#"FlashCards" ofType:#"pdf"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"application/pdf" fileName:#"FlashCards"];
[picker setSubject:#"Flashcards"];
[picker setToRecipients:recipients];
[picker setMessageBody:#"Hello" isHTML:NO];
[picker setMailComposeDelegate:self];
[self presentViewController:picker animated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
The email is being sent and it seems that everything is working, but it's not sending the PDF file.
EDIT:
This actually works, I had other problems in other parts of my code. My problem is solved. I also made sure that the extensions were correct instead of having FlashCards as a file name, it should be FlashCards.pdf This is the exact code I have working:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
NSArray *recipients = #[#"android.aaron.david#gmail.com"];
NSString *path = [[NSBundle mainBundle] pathForResource:#"FlashCards" ofType:#"pdf"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"application/pdf" fileName:#"FlashCards.pdf"];
[picker setSubject:#"Flashcards"];
[picker setToRecipients:recipients];
[picker setMessageBody:#"Hello" isHTML:NO];
[picker setMailComposeDelegate:self];
[self presentViewController:picker animated:YES completion:nil];
Use MIME type text/x-pdf instead of application/pdf. Also check the size/length of myData to verify that the PDF was loaded.
I can send a .csv file as an attachment from my app, but I'd like to shorten the name file for that attachment, because there will be a pile of csv files delivered to the recipient.
Piece of code:
...
if (dorsalesPorTramoYcontrol && dorsalesPorTramoYcontrol.count )
{
NSMutableString *mainString = [[ NSMutableString alloc]initWithString:#"dorsal,paso,tiempo\n"];
for (NSManagedObject *get in dorsalesPorTramoYcontrol) {
//dorsales
NSString *string =[get valueForKey:#"dorsal"];
[mainString appendFormat:#"%#,",string];
//paso
string = [get valueForKey:#"paso"];
string=[string stringByReplacingOccurrencesOfString:#"\"" withString:#"\"\""];
[mainString appendFormat:#"%#,",string];
//tiempo
string = [get valueForKey:#"tiempo"];
string=[string stringByReplacingOccurrencesOfString:#"\"" withString:#"\"\""];
[mainString appendFormat:#"%#",string];
[mainString appendFormat:#"\n"];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
file = [NSString stringWithFormat:#"%#/Tramo%#Control%#.csv", documentsDirectoryPath,section,control];
NSError *csVerror= NULL;
BOOL written = [mainString writeToFile:file atomically:YES encoding:NSUTF8StringEncoding error:&csVerror];
if (!written) {
NSLog( #"Writing failed, error = %#",csVerror);
}else {
NSLog(#"Data saved! File path = %#",file);
[self composeEmail];
}
}
}
-(void)composeEmail{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:[NSString stringWithFormat:#"Resultados Tramo: %# - Control: %#", section, control]];
NSArray *toRecipients = [NSArray arrayWithObjects:#"somebodymail#mail.com", nil];
[mailer setToRecipients:toRecipients];
// Logo
UIImage *myImage = [UIImage imageNamed:#"logo.png"];
NSData *imageData = UIImagePNGRepresentation(myImage);
[mailer addAttachmentData:imageData mimeType:#"image/png" fileName:#"Icon"];
[mailer addAttachmentData:[NSData dataWithContentsOfFile:file] mimeType:#"text/csv" fileName:file];
NSString *emailBody =
[NSString stringWithFormat:#"Resultados Tramo: %# - Control: %# \nDorsal - Paso - Tiempo", section, control];
[mailer setMessageBody:emailBody isHTML:NO];
mailer.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentViewController:mailer animated:YES completion:nil];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
}
}
The filename is the following:
<_var_mobile_Applications_BE8CE610-A83E-4C79-8B9C-0263FA6881D6_Documents_Tramo2Control4.csv>
and I'd like it to be just "Tramo2Control4.csv"
Could you please offer some suggestions to get it?
Change the following line:
[mailer addAttachmentData:[NSData dataWithContentsOfFile:file] mimeType:#"text/csv" fileName:file];
To:
NSString *fileName = [file lastPathComponent];
[mailer addAttachmentData:[NSData dataWithContentsOfFile:file] mimeType:#"text/csv" fileName:fileName];
You were originally setting the file name to be the entire file path.
When I call Apple's MFMailComposeViewController class to send an email from my app, the placement of the To, Cc, Bcc and Subject is a little off. They appear about 1/2 the font size down further than they are supposed to be (when you click the edit the subject, for example, you can only see the top half of the text). This happens even when I copy and paste Apple's sample right into my code. Has anyone seen this before? I've been searching through forums and can't see anybody else who has experienced this.
Code I'm using
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];
// Fill out the email body text
NSString *emailBody = #"It is raining in sunny California!";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
Try the below code:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString * subj = [NSString stringWithFormat:#"" ];
[picker setSubject:subj];
// Set up recipients
NSArray *toRecipients = [[NSArray alloc] initWithObjects:#"first#example.com",nil];
NSArray *ccRecipients = [[NSArray alloc] initWithObjects::#"second#example.com",#"third#example.com", nil];
NSArray *bccRecipients = [[NSArray alloc] initWithObjects::#"fourth#example.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients]
[toRecipients release];
[ccRecipients release];
[bccRecipients release];
NSString *body = #"";
[picker setMessageBody:body isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];