How to Target MailTo Email Clients

by on June 8, 2011

Creating a basic mailto link is a fairly common HTML task. Most of us have implemented the basic format, but there are actually a few options available. Today, we'll review this mailto protocol … and extend it with a PHP function.

The HTML mailto standard automatically uses your visitor's default email client. So … we'll also discuss how to specifically target your email links for the three most-used web-based email clients: gMail, Hotmail, and Yahoo! mail. These additional URL structures are not well known. However, we'll not only reveal them; but, we'll also develop a PHP function which allows you to choose your email client and then output the link on your web page.

Basic MailTo Structure

Click the links in these examples to view the action of the HTML source code. And, note that your own default system email program will be utilized.

Simple Email

<a href="mailto:AnEmailAccount@gmail.com">Simple Email</a>


With a Name (notice that the email address is enclosed in &lt; and &gt;)

<a href="mailto:Luke&lt;AnEmailAccount@gmail.com&gt;">With a Name</a>


With Multiple Recipients (separated by a comma)

<a href="mailto:AnEmailAccount@gmail.com,another_email@hotmail.com">With Multiple Recipients</a>


With a CC (carbon copy)

<a href="mailto:AnEmailAccount@gmail.com?cc=another_email@hotmail.com">With a CC</a>


With a BCC (blind carbon copy)

<a href="mailto:AnEmailAccount@gmail.com?bcc=another_email@hotmail.com">With a BCC</a>


With a Simple Message

<a href="mailto:AnEmailAccount@gmail.com?body=My simple message.">With a Simple Message</a>


With a Simple Message and Subject

<a href="mailto:AnEmailAccount@gmail.com?subject=Email Test Subject&body=My simple message again.">With a Simple Message and Subject</a>


With a Multiline Message (%ØA is used for each linefeed, zero A)

<a href="mailto:AnEmailAccount@gmail.com?body=My multiline%0A%0A message is here%0Ato see.">With a Simple Message</a>


The available parameters for the email link are subject, cc, bcc, and body. You can insert them in any sequence. However, the first one must be preceded by ?. And, any subsequent parameters must be preceded by &.

Years ago, it was necessary to convert body spaces to either a + or to %2Ø. This extra step is no longer required for any modern email client. And, in recent years, modern email clients have advanced to accept the syntax mentioned above as a standard.

Also, the maximum number of characters allowed in a mailto string is 255. So … this trick is essentially for short text messages. But, it is clearly a convenience for your visitors.

If you're concerned about spam-bots grabbing your email address, here are two online utilities that will obfuscate your email address. They each use javascript to accomplish the task.

Targeting Specific Web-based Clients

The PHP function we'll present allows you to easily create email links like the following which target specific email clients: the system default, gMail, Hotmail, or Yahoo! mail. Click each link to verify that it opens the specified email client. If you're not logged into the web-based clients already, you'll be prompted to do so.

Default Client  gMail Client   HotMail Client  Yahoo! Client

Now, let's take a look at the PHP code that creates the special links for us.

Code: PHPwcs_mailto_ex()

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function wcs_mailto_ex($mailto='', $subject='', $body='', $client='', $link_text='', $link_title='', $at_replace='&#64;')
{
    // init
    $subject = rawurlencode(strip_tags($subject));
    $body = str_replace('\r\n', '%0A', $body);
    $body = str_replace('\n', '%0A', $body);
    if (!$link_text) {$link_text = $mailto;}
    $link_text = str_replace('@', $at_replace, $link_text);
    $client = strtolower($client);
 
    // default parameters (system mail: Outlook, Thunderbird, etc.)
    $email['url'] = 'mailto:' . $mailto . '?subject=' . $subject . '&amp;body=' . $body;
    $email['width'] = 0;
    $email['height'] = 0;
    $email['scrollbars'] = 0;
 
    // constuct client-specific parameters
    switch($client)
    {
        case 'gmail':
        case 'g mail':
        case 'google mail':
        case 'google':
            $email['url'] = 'https://mail.google.com/mail/?view=cm&fs=1&tf=1&source=mailto&shva=1&to=' . $mailto . '&amp;su=' . $subject . '&amp;body=' . $body;
            $email['width'] = 700;
            $email['height'] = 500;
            $email['scrollbars'] = 1;
            break;
        case 'hotmail':
        case 'hmail':
        case 'livemail':
        case 'live mail':
            $email['url'] = 'http://mail.live.com/?rru=compose&amp;to=' . $mailto . '&amp;subject=' . $subject . '&amp;body=' . $body;
            $email['width'] = 850;
            $email['height'] = 550;
            $email['scrollbars'] = 1;
            break;
        case 'yahoo mail':
        case 'ymail':
        case 'yahoo':
            $body = str_replace('%0A', '<br>', $body);
            $body = urlencode(urlencode($body));
            $email['url'] = 'http://compose.mail.yahoo.com?to=' . $mailto . '&subject=' . rawurlencode($subject) . '&amp;body=' . $body;
            $email['width'] = 750;
            $email['height'] = 625;
            $email['scrollbars'] = 1;
            break;
    }
 
    // prep for popup
    $wdw_name = 'wcs_mailto_ex_wdw';
    $wdw_features = "scrollbars=$scrollbars,status=0,toolbar=0,location=0,directories=0,menubar=0,resizable=1,width=";
    $url = $email['url'];
    $width = $email['width'];
    $height = $email['height'];
    $scrollbars = $email['scrollbars'];
 
    // determine if display should be a popup window
    if ($email['width'])
    {
        $javascript = "window.open('$url', '$wdw_name', '$wdw_features$width,height=$height');return false;";
        $output = "<a rel='nofollow' style='cursor:pointer;' onclick=\"$javascript\" title='$title'>" . $link_text . "</a>";
    }
    else
    {
        $output = '<a href="' . $url . '" rel="nofollow" title="' . $link_title . '">' . $link_text . '</a>';
    }
 
    // exit
    echo $output;
}
 
    /**********************************************************************
     Copyright © 2011 Gizmo Digital Fusion (http://wpCodeSnippets.info)
     you can redistribute and/or modify this code under the terms of the
     GNU GPL v2: http://www.gnu.org/licenses/gpl-2.0.html
    **********************************************************************/

Our function can take up to seven parameters (which are descriptively named). But, only the first four are needed: $mailto, $subject, $body, and $client.

If you don't set the $link_text parameter, it will default to the $mailto parameter's value, i.e., the email address. Moreover, you can also use an image url for the $link_text value.

In lines 4 -9, we make minor preparatory modifications to the incoming variables. In lines 12 – 15, we prepare to process the system default email client — a normal mailto link.

In lines 18 – 48, we prepare each of the web-based clients to open in a popup window. Each url element reveals the secret structure for targeting these specific clients. Notice, though, that we have to double-encode the Yahoo! email body. That's because this service redirects to a locale-specific client decoding at each step.

In lines 50 – 67, we construct either the web-based popup window or the system default link. Finally, we output the link content in line 70.

Now that the secret is out, you might find other implementations for targeting email clients in your projects.

Here's how we displayed the gMail  link above:

Code: PHP

wcs_mailto_ex('AnEmailAccount@gmail.com',
                'Test Subject Line',
                'This is a sample\n\nemail for testing.\n\nBest regards,\nme',
                'gmail',
                'gMail Client'
                );

 

Share This Article: “How to Target MailTo Email Clients”

(Also Available: Press CTRL+D to Bookmark this Page)

Comments

Share Your Thoughts  One Response to “How to Target MailTo Email Clients”
  1. 1

Share Your Thoughts

(Some editor features are restricted unless you're logged in.)

(When replying to a specific comment, your browser may require Shift+Enter instead of just Enter.)


(get a gravatar)


Notify me of followup comments via e-mail. You can also subscribe without commenting.