Sending Email from Flash (mailto:)

ActionScript 2.0 Flash

Sooner or later, you’re going to want to send email from your SWF.  There are at least two basic ways to accomplish this goal:  the mailto: protocol and a server-side form mailer.  Between the two, the server-side approach is the safer bet.  The reason is because mailto: requires that the user has email software installed — and that this software be configured as the user’s default email handler.  For most home and office users, this might be Outlook, Outlook Express, Thunderbird, Eudora, etc., the list goes on.  Using mailto: in Flash is essentially no different from using mailto: in the href attribute of an anchor tag (a element) in HTML.  It’s certainly an acceptable enough approach, but keep in mind, many people access the Internet through their local library, which means they may not even have email software installed, much less configured for personal use.  So, now that the disclaimer has been stated, let’s see how to use mailto: in cahoots with getURL() to send email from Flash. 

An answer, short and sweet

Assuming you want to trigger your email from a button, give your button an instance name via the Property inspector — here, we’ll call it btnSendEmail — and enter the following ActionScript in a frame of your scripts layer:

btnSendEmail.onRelease = function() {
 getURL("mailto:name@domain.com");
};

How it works

The heart of the matter is that single line in the middle …

getURL("mailto:name@domain.com");

… which launches the user’s email software.  The HTML equivalent does the same thing:

<a href="mailto:name@domain.com">Send email!</a>

The getURL() function requires at least one parameter.  This parameter is a string, hence the quotation marks, and is perhaps most often used with the http: protocol to send the browser to another URL.  Well, mailto: happens to be another protocol Flash supports, and its purpose is to handle email.  Pretty painless, huh?

Don’t forget to replace name@domain.com with the actual email address desired.

Pushing the envelope

Couldn’t resist the pun.  Get it?  ;)   Okay, if you want to get sophisticated, you can actually pre-populate the user’s email with additional information, such as a subject line or even a message in the email body.  The above disclaimer applies all the more now, because not all email software supports what I’m about to show.  But what the heck, right?  Let’s see what the possibilities are, and I’ll leave it up to you to decide where and when to use them.

Note!  Almost a year after writing this article, it came to my attention that the extra parameters here — cc, bcc, subject and body — stop working after Flash Player 7 in locally played SWFs.  Starting with Flash Player 8, only the to: field is properly filled in unless tested in a browser.  I‘m leaving the rest of the article for historical purposes, but take heed.  Thanks, Anton, for bringing this to my attention!

Additional information can be attached to the mailto: content in the form of a query string with name/value pairs.  These name/value pairs appear in the format name=value and are separated by an asterisk (&).  So, the mailto: line appears as before, then you follow with a question mark, then your name/value pairs.  For illustrative purposes, I’m putting certain parts in bold, just so they show up better.

Email with carbon copy (CC)
getURL("mailto:bill@excellent.com?cc=ted@excellent.com");

Email with blind carbon copy (BCC)
getURL("mailto:bill@excellent.com?bcc=ted@excellent.com");

Email with both CC and BCC
getURL("mailto:bill@excellent.com?cc=ted@excellent.com&
bcc=rufus@future.com");

Email with subject
getURL("mailto:bill@excellent.com?subject=
Strange%20things%20are%20afoot%20at%20the%20Circle%2dK%2e");

Whoa, what’s going on in that last one?  Shouldn’t that simply say “Strings things are afoot at the Circle-K”?  What are all those %20s?  You may have seen similar formatting in query strings elsewhere, such as the address bar of a browser.  %20 happens to be a hexadecimal number.  In this case, the % indicates “Hey, this is hexadecimal,” and the 20 indicates 32 in the decimal system.  In other words, this is what we humans call “number 32,” which happens to represent the space character in urlencoding.

It’s a good idea to urlencode the subject and body parts of your mailto: messages.  Certain punctuation — such as spaces, question marks, and ampersands, for example — or other special characters might break your messages, otherwise.  You can do it by hand, by using this urlencode chart, or you can use ActionScript’s handy escape() function as follows:

getURL("mailto:bill@excellent.com?subject=" +
escape("Strange things are afoot at the Circle-K."));

Note that the mailto: string terminates at subject=.  We use the addition operator to concatenate the return value of escape(), which is supplied with its own string..  All of this is wrapped inside the parentheses used by getURL().

You can even include input from the user this way.  Assuming a text field with the instance name tfBody, you could expand that last subject example to include body text as well.  Watch carefully to see that we’re still ultimately using name/value pairs.  In this past example, the query string begins with the question mark, as always, then contains a subject and body component separated by an ampersand.

getURL("mailto:bill@excellent.com?subject=" +
escape("Strange things are afoot at the Circle-K.") + "&body=" +
escape(tfBody.text));

Leave a Reply