Sending email on iPhone SDK app

March 24, 2009 § Leave a comment

It is another tricky area to send email from your iphone SDK app.

The only method you can use to email is openURL:. This method within UIKit supports http:, https:, tel:, mailto:.

– (BOOL)openURL:(NSURL *)url

The URL can locate a resource in the same or other application. If the resource is another application, invoking this method may cause the calling application to quit so the other one can be launched.

Basically you are asking your users to leave your application if you include email feature on your app.

– (IBAction) emailButton:(id)sender {

NSString *toText = @””;
NSString *to = [toText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *subjectText = @”email subject”;
NSString *subject = [subjectText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *bodyText = @”email body”;
NSString *body = [bodyText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString *urlString =
[NSString stringWithFormat:@”mailto:?to=%@&subject=%@&body=%@”, to, subject, body];
NSURL *url = [[NSURL alloc] initWithString:urlString];
[[UIApplication sharedApplication] openURL:url];


Another example of using HTML within body

NSString *body = @”<table><tr><td style=’text-align:right’><b>First Name</b>:</td><td>Bess</td></tr><tr><td style=’text-align:right’><b>Last Name</b>:</td><td>Ho</td></tr><tr><td style=’text-align:right’><b>Occupation:<b/></td><td>iPhone Developer</td></tr></table>”;

Using openURL: method won’t allow you to attach any file to the email. Only SMTP client method or server-side method would support email attachment.

Other options are 1) code your own mail client (SMTP) inside your app or 2) have a server to send the mail for you. You can code the app to invoke a URL on your server that would send the mail for you. You call the URL from your code.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Sending email on iPhone SDK app at Web Builders.


%d bloggers like this: