Html mailto 特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/987199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 23:53:57  来源:igfitidea点击:

mailto special characters

htmlmailto

提问by Arve Skj?restad

Is there a way to make the email client ( Outlook ) accept special characters coming from the mailto link in html? I'm trying to have a mailto link with german characters in the body, but in Outlook I get only strange characters.

有没有办法让电子邮件客户端( Outlook )接受来自 html 中 mailto 链接的特殊字符?我正在尝试在正文中使用带有德语字符的 mailto 链接,但在 Outlook 中我只得到奇怪的字符。

Thanks

谢谢

回答by Arve Skj?restad

I just spent 2 days investigation this issue. Our issue was that mailto: links on our utf-8 encoded web pages did not work for Outlook users if the subject= string contained non-ascii characters, like e.g Norwegian characters. An example is: "mailto:[email protected]?subject=julegl?gg og f?rik?l"

我只花了 2 天时间调查这个问题。我们的问题是如果 subject= 字符串包含非 ascii 字符(例如挪威字符),则我们的 utf-8 编码网页上的 mailto: 链接对 Outlook 用户不起作用。例如:“mailto:[email protected]?subject=julegl?gg og f?rik?l”

From what I have learned so far, Outlook simply does not handle anything other than ASCII and iso-8859-1 characters. So when trying to click on the above mailto link (either from IE or Firefox), Outlook fails to decode the characters, leaving the subject broken and containing "weird" characters.

从我目前了解到的情况来看,Outlook 不会处理除 ASCII 和 iso-8859-1 字符以外的任何内容。因此,当尝试单击上述 mailto 链接(来自 IE 或 Firefox)时,Outlook 无法对字符进行解码,从而导致主题损坏并包含“奇怪”字符。

So the next step was to try to re-encode the pages in ISO-8859-1. What we did was to replace the original mailto link on the utf-8 page with a link to a "email-to-iso"-service, like this:

所以下一步是尝试重新编码 ISO-8859-1 中的页面。我们所做的是将 utf-8 页面上的原始 mailto 链接替换为指向“email-to-iso”服务的链接,如下所示:

http://url.com/service.php?service=util.mailtoencode&mailto=mail%40coretrek.no%3Fsubject%3Demne+%C3%B8%C3%A6%C3%A5+emne

http://url.com/service.php?service=util.mailtoencode&mailto=mail%40coretrek.no%3Fsubject%3Demne+%C3%B8%C3%A6%C3%A5+emne

This page would convert the mailto characters to iso-8859-1 and then output the entire page content in iso-8859-1. A javascript on the page, containing "location.href='mailto:...'" was used to open the client's email client automatically.

此页面会将mailto 字符转换为iso-8859-1,然后以iso-8859-1 格式输出整个页面内容。页面上包含“location.href='mailto:...'”的 javascript 用于自动打开客户端的电子邮件客户端。

So far everything seemed ok. This actually works in Internet Explorer, both with Thunderbird and Outlook (tested on IE7 on WinXP with Outlook express and TB 2).

到目前为止,一切似乎都很好。这实际上适用于 Internet Explorer,包括 Thunderbird 和 Outlook(在带有 Outlook express 和 TB 2 的 WinXP 上的 IE7 上测试)。

BUT the problem now is actually Firefox. It seems like Firefox is unable to decode url-encoded urls containing characters found only in ISO-8859-1 but not in ASCII (like the norwegian ?, represented by %E5 when encoded). The same ? is handled correct if the page encoding is utf-8, but it seems like the Firefox developers have forgotten to test special characters together with the ISO-8859-1 charset. The result is that Firefox passes an un-decoded string (still containing %E5 intstead of ?) to the email client. And, amazingly, this is handled correct by Outlook (which manages to decode the string itself), but NOT by Thunderbird, which probably has the same bug as Firefox. If you DON't url encode the subject, the string is passed correctly to Thunderbird, but not to Outlook.

但现在的问题实际上是 Firefox。似乎 Firefox 无法解码包含仅在 ISO-8859-1 中找到但不在 ASCII 中找到的字符的 url 编码 url(如挪威语 ?,编码时由 %E5 表示)。相同 ?如果页面编码为 utf-8,则处理正确,但 Firefox 开发人员似乎忘记将特殊字符与 ISO-8859-1 字符集一起测试。结果是 Firefox 将一个未解码的字符串(仍然包含 %E5 而不是 ?)传递给电子邮件客户端。而且,令人惊讶的是,这被 Outlook(它设法自己解码字符串)正确处理,而不是由 Thunderbird 正确处理,Thunderbird 可能与 Firefox 有相同的错误。如果您不对主题进行 url 编码,则字符串会正确传递给 Thunderbird,但不会传递给 Outlook。

We have also been trying other encoding methods, like php's htmlentities, htmlspecialchars, base64 encoding etc, but all of them fails one way or the other.

我们也一直在尝试其他编码方法,如 php 的 htmlentities、htmlspecialchars、base64 编码等,但它们都以一种或另一种方式失败。

So, summarized:

所以总结一下:

Pages encoded in utf-8:

以 utf-8 编码的页面:

IE fails always

IE 总是失败

FF -> Thunderbird: OK

FF -> 雷鸟:好的

FF -> Outlook: FAIL

FF -> 展望:失败

Pages encoded in iso-8859-1:

以 iso-8859-1 编码的页面:

IE: OK

伊:好的

FF -> Thunderbird: Fails if subject is url encoded, ok if not)

FF -> Thunderbird:如果主题是 url 编码,则失败,如果不是,则可以)

FF -> Outlook: Fails if subject is not url encoded, ok if encoded) (this is Windows, on Ubuntu Linux FF and TB works OK always).

FF -> Outlook:如果主题不是 url 编码,则失败,如果已编码则可以)(这是 Windows,在 Ubuntu Linux FF 和 TB 上始终正常工作)。

Hoping this was helpful for others having the same problem.

希望这对有同样问题的其他人有所帮助。

回答by Mario Chueca

In PHP I think the function that works best with Outlook is rawurlencode()

在 PHP 中,我认为最适合 Outlook 的函数是 rawurlencode()

回答by yandr

Actually, the solution is http://blogs.msdn.com/ie/archive/2007/02/12/International-Mailto-URIs-in-IE7.aspxand it is not nice.

实际上,解决方案是http://blogs.msdn.com/ie/archive/2007/02/12/International-Mailto-URIs-in-IE7.aspx并不好。

Basically, in IE 7 and 8 the user must have enabled an advanced setting in Internet Options, something that 100% of the users will not know will not have enabled.

基本上,在 IE 7 和 8 中,用户必须在 Internet 选项中启用高级设置,100% 的用户不知道将不会启用。

回答by John Weldon

I think using a urlencode method should do what you're looking for. JavaScript has .encodeURI() methods on string objects, and .NET has the HttpUtility.UrlEncodemethod.

我认为使用 urlencode 方法应该可以满足您的需求。JavaScript 在字符串对象上有 .encodeURI() 方法,而 .NET 有HttpUtility.UrlEncode方法。

What language are you using?

你使用什么语言?

回答by Dean Rather

You need to enable UTF-8 support for the mailto: protocol

您需要为 mailto: 协议启用 UTF-8 支持

From the main outlook window, click Tools -> Options -> mail format -> international options -> "Enable UTF-8 support for mailto: protocol".

在 Outlook 主窗口中,单击工具 -> 选项 -> 邮件格式 -> 国际选项 ->“为 mailto: 协议启用 UTF-8 支持”。

回答by Kiran Gohel

rawurlencode()function works best with outlook, tested with Firefox, Chrome & IE

rawurlencode()功能在 Outlook 中效果最佳,已通过 Firefox、Chrome 和 IE 测试

回答by ghoti

As yandr indicated, this issue is an ongoing problem with Outlook.

正如 yandr 所指出的,这个问题是 Outlook 的一个持续问题。

Microsoft has published documentationthat states that properly configured Outlook 2003 and 2007 attached to a properly configured Exchange server will default to supporting Unicode, but that doesn't really help you with the general public.

Microsoft 已发布文档指出,正确配置的 Outlook 2003 和 2007 附加到正确配置的 Exchange 服务器将默认支持 Unicode,但这并不能真正帮助您与普通大众。

For reference, the "standard" you want to refer to for this is RFC 2047.

作为参考,您要参考的“标准”是RFC 2047

The solution that I have implemented to get around this limitation (with Swedish, actually) is to use a web form instead of a mailto: link. It requires more setup on the server side, but gives you a lot more control over the contact process.

我为解决此限制而实施的解决方案(实际上是瑞典语)是使用 Web 表单而不是 mailto: 链接。它需要在服务器端进行更多设置,但可以让您更好地控制联系过程。

I'm sure this isn't what you wanted to hear, but until the world stops using broken software from Microsoft, we'll continue to need workarounds like this.

我敢肯定这不是您想听到的,但是在全世界停止使用 Microsoft 的损坏软件之前,我们将继续需要这样的解决方法。

回答by PelleLauritsen

If one is using SharePoint 2010, it seems Microsoft has been aware of this issue, and has supplied some functions to solve this.

如果使用的是 SharePoint 2010,那么 Microsoft 似乎已经意识到了这个问题,并提供了一些功能来解决这个问题。

The following will properly escape the link to the current page

以下将正确转义当前页面的链接

escapeProperly(escapeProperlyCoreCore($(location).attr('href'), false, false, true))

escapeProperly(escapeProperlyCoreCore($(location).attr('href'), false, false, true))

回答by Saliya Wicky

In JavaScript you can use encodeURIComponentfunction for subject and body. Then it will show all special characters in email.

在 JavaScript 中,您可以encodeURIComponent对主题和正文使用函数。然后它将显示电子邮件中的所有特殊字符。

const emailRequest = {
    to: "[email protected]",
    cc: "[email protected]",
    subject: "Email Request - for <CompanyName>",
    body: `Hi All, \r\n \r\n This is my company <CompanyName> 
            \r\n Thanks`,
};

const subject = encodeURIComponent(emailRequest.subject.replace("<CompanyName>", 'ABC & ** Company'));
const body = encodeURIComponent(emailRequest.body.replace("<CompanyName>", 'ABC & ** Company'));
window.location.href = (`mailto:${emailRequest.to}?cc=${emailRequest.cc}&subject=${subject}&body=${body}`);

回答by chaos

It sounds like you need the page containing the mailto link to be in the encoding that Outlook is expecting. Without knowing any more about the situation, I'd try encoding the page in UTF-8 and ISO-8859-1.

听起来您需要包含 mailto 链接的页面采用 Outlook 期望的编码。在不了解更多情况的情况下,我会尝试使用 UTF-8 和 ISO-8859-1 对页面进行编码。

The relevant 'more about the situation' would be what weird characters appear and what the page's encoding is currently.

相关的“有关情况的更多信息”将是出现什么奇怪的字符以及当前页面的编码是什么。