iOS 5 simplest way to send email from an app - iphone

I just tried to send email using this code and it seems to work:
NSString* urlEmail = [NSString stringWithString: #"mailto:foo#example.com?cc=bar#example.com&subject=test%20from%20me!&body=this%20is%20a%20test!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: urlEmail]];
The only problem is, is there a function I can use to automatically escape everything in a normal NSString so I don't have to write it out manually like this? If not, how can I use stringWithFormat without conflicting with the % signs already in the string? I basically want to be able to append to, subject, body, etc. dynamically.

Use MFMailComposeViewController:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setToRecipients:recipients];
[picker setSubject:subject];
[picker setMessageBody:body isHTML:YES];
[self presentModalViewController:picker animated:YES];
[picker release];

According to https://stackoverflow.com/a/917630/535632, you can escape like this:
NSString *urlStr = [urlEmail stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Related

How to attach screenshot to mailcomposer in iOS

Hi i'm using the below code to attach screenshot to mailcomposer, i'm not having a device to check so will this work on actual device?
-(void)launchMailAppOnDevice
{
/*Take a SnapShot of current screen*/
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * imageData = UIImageJPEGRepresentation(image, 1.0);
NSString *recipients = #"mailto:xyz#abc.com?cc=#\"\"&subject=blah!!blah!!";
NSString *body = #"&body=blah!!blah!!";
NSString *email = [NSString stringWithFormat:#"%#%#%#", recipients, body, imageData];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
The last 5 lines are wrong. You most likely want to use the MFMailComposeViewController class:
MFMailComposeViewController *mcv = [[MFMailComposeViewController alloc] init];
[mcv setSubject:#"blah!!blah!!"];
[mcv setToRecipients:[NSArray arrayWithObject:#"xyz#abc.com"]];
[mcv setMessageBody:#"Blah!! 'tis the body!" isHTML:NO];
[mcv addAttachmentData:imageData mimeType:#"image/jpeg" fileName:#"Screenshot.jpg"];
[someViewController presentModalViewController:mcv animated:YES];
[mcv release];
P. s.: don't forget to add the MessageUI framework to your project and also #import <MessageUI/MessageUI.h>.
P. p. s.: while it's important to test on an actual device, it's even more important to read some guides and documentation before writing the actual code.
You'll want to add:
NSData *imageData = UIImagePNGRepresentation(image);
[picker addAttachmentData:imageData mimeType:#"image/png" fileName:#"fileName"];
if you plan on using a MFMailComposeViewController, which you should use instead of mailto:, but I can't stress enough how important it is to always test your applications on a real device.

MFMailComposerViewController not recognizing URL as link

MFMailComposerViewController displays a URL in its message body as plain text and not as a tappable link. Is there anyway I can make this happen?
Set is as HTML with The link using – setMessageBody:isHTML:. Plain is as it is called, plain.
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = self;
[composer setMessageBody:message isHTML:YES];
NSMutableString *body = [NSMutableString string];
[body appendString:#"<h1>Hello User!</h1>\n"];
[body appendString:#"Click Me!\n"];
[composer setMessageBody:body isHTML:YES];
hope this works for u. happy coding :)
Yes It is possible and you can do it this way:
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init]; composer.mailComposeDelegate = self;
[composer setSubject:subject];
[composer setMessageBody:message isHTML:YES];
//Message is just a NSString with HTML Content and you can have all the HTML Contents in it.

Mailbox programming in iphone?

I want saved email addresses of different users, and I want to open mailBox's new mail when I click the email, same as phonebook email in iphone,
what should I to do ???
You should use the MFMailComposeViewController class, and the implement MFMailComposeViewControllerDelegate protocol,
First to send a message:
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:#"My Subject"];
[controller setMessageBody:#"Hello there." isHTML:NO];
[self presentModalViewController:controller animated:YES];
[controller release];
Once you sent you will get delegate callback in mailComposeController :
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error;
{
if (result == MFMailComposeResultSent) {
NSLog(#"It's gone!");
}
[self dismissModalViewControllerAnimated:YES];
}
Create new message within Mail:
NSString *subject = #"The subject";
NSString *body = #"The message";
NSString *address = #"mail#address.com";
NSString *cc = #"mail#address.com";
NSString *path = [NSString stringWithFormat:#"mailto:%#?cc=%#&subject=%#&body=%#", address, cc, subject, body];
NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];

Email app from another app

I want to start an email app from app on button pressed. But when I pressed the button nothing is happening !!!
code :
- (IBAction) startMail
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"mailto:emailAdress?subject=testMail&body=its test mail."]];
}
any thing wrong in code ? Also button is properly set in IB.
Thanks..
If you want to compose an EMail inside your app, you should have a look at the MFMailComposeViewController reference to do so instead of calling a mailto: URL scheme.
Using the mailto URL won't work in the simulator as mail.app isn't installed on the simulator. It does work on device though.
You can use "MFMailComposeViewController" to send mail from your application.
Sample code-
MFMailComposeViewController *picker;
picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString *objSubject = [[NSString alloc] init];<br/>
NSString *emailBody = [[NSString alloc] init];
[picker setSubject:objSubject];
[picker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:picker animated:YES];
You must use the following delegate to check the status of mail sent or not
**-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
}**
Enjoy
Try this:
NSString *mailString = [NSString stringWithFormat:#"mailto:?to=%#&subject=%#&body=%#",
[to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailString]];
Here to, subject and body are NSString variables
Check this out, it's a better solution than directing your user out of the app;
How can I send mail from an iPhone application
NSString *url = [NSString stringWithString: #"mailto:foo#example.com?cc=bar#example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

Not receiving attachments sent from MFMailComposer

I am trying to send out an email with a .csv attachment on the iPad. I am using the MFMailComposer per many examples given which is displayed below. When sending out the email, I can see the correct file attachment in the MFMailComposer window, but when I receive the email, nothing is attached. Any guidance as to what I may be doing wrong would be appreciated. Thank you for your time,
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:[NSString stringWithFormat:#"Results for Participant %d.", [delegate participantNumber]]];
[mailViewController setMessageBody:[NSString stringWithFormat:#"The results for Participant %d in Study: %# are as follows:", [delegate participantNumber], [[delegate getAccountData:([delegate accountItems] * [delegate accountNumberInUse])] description]] isHTML:NO];
NSData *textData = [[NSData alloc] initWithContentsOfFile:dataFileName];
[mailViewController addAttachmentData:textData mimeType:#"text/csv" fileName:[NSString stringWithFormat:#"Participant_Info_#%d.csv", [delegate participantNumber]]];
[self presentModalViewController:mailViewController animated:YES];
[mailViewController release];
}
Check the content of the textData variable, also (just guessing from your code) the dataFileName needs to contain a full system path to the file, not just it's name!
NSString *dataFilePath = [[NSBundle mainBundle] pathForResource:dataFileName ofType:nil];