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

标签 jquery html mailto email-client

我一直在寻找此功能,是否可以在用户单击mailto时添加电子邮件客户端选择器:例如,我正在使用以下电子邮件代码

<a href="mailto:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="452028242c290528206b262a28" rel="noreferrer noopener nofollow">[email protected]</a>">Email Us</a>

如果用户想使用 gmail/yahoo/hotmail 而不是默认的电子邮件客户端(例如 Outlook)该怎么办? 我找到了similar question之前也问过。但我需要知道在任何情况下是否可以使用任何 jQuery 插件或 HTML,无论它是什么。

最佳答案

您无法使用“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('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2d6c4368404c44416c4e4e425843596d4a404c4441034e4240" rel="noreferrer noopener nofollow">[email protected]</a>',
            '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

关于jquery - 如何提示用户为 mailto 属性选择电子邮件客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13115787/

相关文章:

jquery - 获取一个 div 的高度并将其应用于单独的一个

jquery - 独立于库/框架的咆哮式通知?

html - 如何测试 Cucumber 中的 mailto 链接?

android - 在 html Gmail/Android chrome 中用空格替换 +

java - 将 html 格式的字符串转换为 mailto 链接

jquery - 使用 SlideDown 显示 div

php - 使用 Wordpress Twenty Eleven 主题的 float 导航菜单

html - 如何在水平线而不是垂直行中显示社交媒体图标

javascript - HTML z-index 未按预期工作 - 元素位置问题?

PHP Sessions,打印 session 信息的问题