Html mailto 中的超链接正文选项不支持某些特殊字符,例如“&”

Html hyperlink body option in mailto does not support some special characters like '&'

我正在使用 hyperlinks 来提供 mailto 功能,我正在为电子邮件动态创建正文,
正文代码

let value = 50;
let titlevar = stack overflow example
let myLink = "http://localhost:5500/abc/def.html?did=" + value + "&title=" + titlevar
let emailstring = `mailto:?Subject=Sometext&body=Click here to view my example:  %0D ` + myLink

我正在使用标签作为,

<a href="` + emailstring + `">

但是在打开这个 link 时,在 Outlook 中,字符串断了。我只得到这个, http://localhost:5500/abc/def.html?did=50

我认为 outlook 正在限制 '&' 这个符号,所以我尝试用逗号 (',') 替换它,它按预期工作

我在 titlevar 中有空格,所以我尝试了 encodeURI(titlevar) 这个 returns stack%20overflow%20example。这也不支持前景。它在 http://localhost:5500/abc/def.html?did=50,title=stack 处中断。

它在 outlook 中不起作用,我没有尝试使用 gmail

您需要使用 encodeURIComponentSubject= 之后的整个 url 值进行编码。

如果主题本身包含 HTML and/or url,那么这些也需要使用 encodeURI 进行编码。是的,这意味着某些部分需要 2 层 encodeURIComponent。

const myLink = "http://localhost:5500/abc/def.html?did=" + value + "&title=" + encodeURIComponent(titlevar);
const body = encodeURIComponent('Click here to view my example: ' + myLink);
const emailstring = `mailto:?Subject=Sometext&body=${body}`;

普遍的问题是,当您将字符串嵌入到 url 中时,您应该始终对它们进行编码。如果该编码字符串嵌入到 另一个 url 中,也对其进行编码。