Send Email from inside an iPhone App

One of our first challenges with the Ninja Missions iPhone app was to make a simple and quick way for users to share missions with their friends. This is a valuable tool for viral marketing. Through past projects, we know how many people use the 'share this with a friend' links on websites. It's a surprisingly effective free marketing tool, so we wanted to add one to our iPhone.

Our first thought was: How cool would it be to allow people to text it to a friend?!

Unfortunately, Apple does not currently have an API that allows you to open a pre-filled SMS like they do for email. The only way to achieve this would be to use a SMS Gateway provider (like TM4B or Clickatell) and send the SMS through a web-call in the application.

We can open the SMS application through a link with the following code, but there is no way to fill in the message automatically. (See: Apple URL Scheme Reference)

1
2
<a href="sms:">Opens SMS Application</a>
<a href="sms:1-800-555-1234">Opens New SMS Message to 1-800-555-1234</a>

Or, open it from within an app with the following code:

1
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:1-800-555-1234"]];

Clearly, just opening a blank text message wasn't going to work in this case, and the price of SMS Gateway Providers couldn't be justified until our app started generating a bit of revenue. So we decided to use the tried and true email link to open a premade email inside of the iPhone Mail application.

To Send Email from inside an iPhone App in Objective-C:

1
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:test@test.com?subject=Subject%20Line&body=Body%20Text"]];

Simply put, the above code is the core function needed to open a new email message from within an iPhone App. However, this was not entirely what we needed. We needed to put a NSString generated from our data into the middle of a fixed chunk of text. To do that, we did this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
- (IBAction)sendEmail {
	/* We want to include this variable text within the premade body. 
	   You could replace this variable text with a personalized message from the user or remove it. */
 
	/* First, encode the variable text (in this case, the mission) 
	   NOTE: self.theMission is our NSString object which contain's the unescaped mission text
	   Replace this variable with your own variable (if needed). */
	NSString *encodedMission = [self.theMission stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 
	/* Make a string that contains the first part of the static text.
	   We start off with the mailto:email?subject=subject&body=text format.
	   Stop right where you want to put in the variable's text.
	   ---------
	   To make this part easy, use a free web-based URL encoder like the one at: 
	   http://meyerweb.com/eric/tools/dencoder/
	   ---------
	   Our first half will give us:
	   To:Test@Test.com
	   Subject: Subject Text Here
	   Body: Fellow Ninja -
	   (line break)
	   */
	NSString *firstHalf = @"mailto:Test@Test.com?subject=Subject%20Text%20Here&body=Fellow%20Ninja%20-%0D%0A%0D%0A";
 
	/* And the second half of the email (the post-variable segment)
	   Encoded the same way we did the first half. This string will decode to:
	   (line break)
	   Yours,
	   (line break)
	   Ninja Master 
	   */
	NSString *lastHalf = @"%0D%0A%0D%0AYours%2C%0D%0A%0D%0ANinja%20Master";
 
	/* Now we concatenate the strings. */
	NSString *mailCode = [NSString stringWithFormat:@"%@%@%@", firstHalf, encodedMission, lastHalf];
 
	/* Now it's time to send it! The following code makes the above string into a 
	   URL and opens the URL causing the Mail program to launch with a premade email. */
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailCode]];
}

We then bind this code to a button action in the interface builder.

It is important to note that this button will not do anything inside the iPhone simulator from the iPhone SDK because the simulator does not have a Mail App. We have to test the app on a developer's iPhone to test the function.

And it works!

iPhone Email Screenshot iPhone Email Screenshot

One Response to “Send Email from inside an iPhone App”

  1. kaustubhv says:

    really helped me, very quick simple solution. Thanks a ton!

Leave a Reply