Search code examples
jqueryhtmlmailtoemail-client

How to prompt user to select email client for mailto attribute


I have been looking for this functionality, if is it possible to add an email client selector when the users click on mailto: For example i am using following code for emails

<a href="mailto:[email protected]">Email Us</a>

What if users want to use gmail/yahoo/hotmail instead of his default email client e.g Outlook. I have found a similar question asked before as well. But i need to know if that is in any case possible either with any jQuery plugin or HTML whatever it may be.


Solution

  • you can't do this with 'mailto' functionality. But you can acheive targeting email clients with the help of php function. Here is the php function to Targeting Specific Web-based Clients:

        Targeting Specific Web-based Clients
    
    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;
    }
    

    To target gmail client call the above function as follows:

        wcs_mailto_ex('[email protected]',
                'Test Subject Line',
                'This is a sample\n\nemail for testing.\n\nBest regards,\nme',
                'gmail',
                'gMail Client'
                );
    

    Source: http://wpcodesnippets.info/blog/how-to-target-mailto-email-clients.html