如何提示用户为mailto属性选择电子邮件客户端

时间:2012-10-29 04:15:48

标签: jquery html mailto email-client

我一直在寻找这个功能,如果可以在用户点击 mailto:时添加电子邮件客户端选择器,例如我使用以下代码发送电子邮件

<a href="mailto:email@me.com">Email Us</a>

如果用户想要使用gmail / yahoo / hotmail而不是他的默认电子邮件客户端,例如Outlook,该怎么办? 我之前也发现了similar question。但我需要知道,无论如何,无论是任何jQuery插件还是HTML,无论如何都可能。

1 个答案:

答案 0 :(得分:3)

使用'mailto'功能无法执行此操作。但是你可以借助php功能实现目标电子邮件客户端。 以下是针对特定基于Web的客户端的php函数:

    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;
}

要定位gmail客户端,请按以下方式调用上述函数:

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

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