How to Open the Mail App With a Pre-Composed HTML Email

Dec 6, 2009   //   by Derek van Vliet   //   Development  //  3 Comments

photo 2-1Have you ever wanted to open the Mail app with a pre-composed email? Then today’s iPhone development advent tip is the one for you! This has many uses, but a common one is a tell-a-friend feature in apps. If you attach the code from this tip to a button, you can give your users a way to promote your app to their friends via the iPhone’s built in Mail app.

A Handy Function

The sendEmailTo function, seen below, can be included in any class in your app. This function gives you a simple interface to send emails. It allows you to define “to” addresses, “cc” addresses, “bcc” addresses, a subject and a body. The body supports HTML tags.

- (void)sendEmailTo:(NSString*)to withCC:(NSString*)cc withBCC:(NSString*)bcc withSubject:(NSString*)subject withBody:(NSString*)body {
	NSString * url = [NSString stringWithFormat:@"mailto:?to=%@&cc=%@&bcc=%@&subject=%@&body=%@",
					  [to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
					  [cc stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
					  [bcc stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
					  [subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
					  [body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
 
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}

NOTE: Running this function in the iPhone Simulator will have no effect because the simulator does not include the iPhone’s Mail app. To see the effects of this function, it must be run on a device.

Example Usage

Once you have that message defined in one of your classes, you can open the Mail app with a pre-composed email with just one message call. The following example opens an email and specifies a to, cc and bcc address. It populates the subject with “Addicus” and populates the body with a short message about Addicus that includes a link and an image (which is also a link). As you can see, this is done with good old HTML tags. This results in the email in the above screenshot.

[self sendEmailTo:@"mom@example.com"
           withCC:@"dad@example.com"
          withBCC:@"asecretfriend@example.com"
      withSubject:@"Addicus"
         withBody:@"<p>Check out this game, <a href='http://addicusgame.com'>Addicus</a>, for the iPhone and iPod Touch. It's super-addictive!</p><p><a href='http://addicusgame.com'><img src='http://getsetgames.com/wp-content/uploads/2009/11/icon_addicus.png'/></a></p>"];

If you would like to have the user specify the to, cc and bcc addresses themselves, as you would in the case of a tell-a-friend button, then you would call the following message:

[self sendEmailTo:@""
           withCC:@""
          withBCC:@""
      withSubject:@"Addicus"
         withBody:@"<p>Check out this game, <a href='http://addicusgame.com'>Addicus</a>, for the iPhone and iPod Touch. It's super-addictive!</p><p><a href='http://addicusgame.com'><img src='http://getsetgames.com/wp-content/uploads/2009/11/icon_addicus.png'/></a></p>"];

3 Comments

  • [...] the trusty openURL method of the UIApplication class. We have previously discussed this method when pre-composing emails and, believe it or not, opening a [...]

  • [...] openURL method, which we have seen before when we discussed how to dial a phone number, pre-compose an email, and yes, even open a [...]

  • How can you actually execute send mail from this code and not even showing a mail application window?

Leave a comment

Our Games

Latest Tweets