带有 HTML 正文的 mailto 链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5620324/
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-29 07:54:11  来源:igfitidea点击:

mailto link with HTML body

htmlemailmailto

提问by GxG

I have a couple of mailtolinks in a HTML document.

mailto在 HTML 文档中有几个链接。

<a href="mailto:etc...">

Can I insert HTML formatted body in the mailto:part of the href?

我可以在 的mailto:部分插入 HTML 格式的正文href吗?

<a href="mailto:[email protected]?subject=Me&body=<b>ME</b>">Mail me</a>

Note that (2016) in iOS, it is perfectly fine to add <i>and <b>tags for simple italic, bold formatting.

请注意 (2016) 在 iOS 中,为简单的斜体、粗体格式添加<i><b>标记是非常好的。

回答by Alfonso Marin

As you can see in RFC 6068, this is not possible at all:

正如您在RFC 6068 中看到的,这根本不可能:

The special <hfname>"body" indicates that the associated <hfvalue>is the body of the message. The "body" field value is intended to contain the content for the first text/plain body part of the message. The "body" pseudo header field is primarily intended for the generation of short text messages for automatic processing (such as "subscribe" messages for mailing lists), not for general MIME bodies.

特殊的<hfname>“body”表示关联的<hfvalue>是消息的正文。“body”字段值旨在包含消息的第一个文本/纯正文部分的内容。“body”伪标头字段主要用于生成用于自动处理的短文本消息(例如邮件列表的“订阅”消息),而不是用于一般 MIME 正文。

回答by Quentin

No. This is not possible at all.

不,这根本不可能。

回答by Oliver Pearmain

Whilst it is NOT possible to use HTML to format your email body you can add line breaks as has been previously suggested.

虽然不可能使用 HTML 来格式化您的电子邮件正文,但您可以按照之前的建议添加换行符。

If you are able to use javascript then "encodeURIComponent()" might be of use like below...

如果您能够使用 javascript,那么“encodeURIComponent()”可能会像下面这样使用...

var formattedBody = "FirstLine \n Second Line \n Third Line";
var mailToLink = "mailto:[email protected]?body=" + encodeURIComponent(formattedBody);
window.location.href = mailToLink;

回答by Andy

I have used this and it seems to work with outlook, not using html but you can format the text with line breaks at least when the body is added as output.

我已经使用了它,它似乎可以与 Outlook 一起使用,而不是使用 html,但至少在将正文添加为输出时,您可以使用换行符格式化文本。

<a href="mailto:[email protected]?subject=Hello world&body=Line one%0DLine two">Email me</a>

回答by Matthew

It's not quite what you want, but it's possible using modern javascript to create an EML file on the client and stream that to the user's file system, which should open a rich email containing HTML in their mail program, such as Outlook:

这不是您想要的,但是可以使用现代 javascript 在客户端上创建一个 EML 文件并将其流式传输到用户的文件系统,这应该在他们的邮件程序中打开一个包含 HTML 的丰富电子邮件,例如 Outlook:

https://stackoverflow.com/a/27971771/8595398

https://stackoverflow.com/a/27971771/8595398

Here's a jsfiddle of an email containing images and tables: https://jsfiddle.net/seanodotcom/yd1n8Lfh/

这是包含图像和表格的电子邮件的 jsfiddle:https://jsfiddle.net/seanodotcom/yd1n8Lfh/

HTML

HTML

<!-- https://jsfiddle.net/seanodotcom/yd1n8Lfh -->
<textarea id="textbox" style="width: 300px; height: 600px;">
To: User <[email protected]>
Subject: Subject
X-Unsent: 1
Content-Type: text/html

<html>
<head>
<style>
    body, html, table {
        font-family: Calibri, Arial, sans-serif;
    }
    .pastdue { color: crimson; }
    table {
        border: 1px solid silver;
        padding: 6px;
    }
    thead {
        text-align: center;
        font-size: 1.2em;
        color: navy;
        background-color: silver;
        font-weight: bold;
    }
    tbody td {
        text-align: center;
    }
</style>
</head>
<body>
<table width=100%>
    <tr>
        <td><img src="http://www.laurell.com/images/logo/laurell_logo_storefront.jpg" width="200" height="57" alt=""></td>
        <td align="right"><h1><span class="pastdue">PAST DUE</span> INVOICE</h1></td>
    </tr>
</table>
<table width=100%>
    <thead>
        <th>Invoice #</th>
        <th>Days Overdue</th>
        <th>Amount Owed</th>
    </thead>
    <tbody>
    <tr>
        <td>OU812</td>
        <td>9</td>
        <td>95.00</td>
    </tr>
    <tr>
        <td>OU812</td>
        <td>9</td>
        <td>95.00</td>
    </tr>
    <tr>
        <td>OU812</td>
        <td>9</td>
        <td>95.00</td>
    </tr>
    </tbody>
</table>
</body>
</html>
</textarea> <br>
<button id="create">Create file</button><br><br>
<a download="message.eml" id="downloadlink" style="display: none">Download</a>

Javascript

Javascript

(function () {
var textFile = null,
  makeTextFile = function (text) {
    var data = new Blob([text], {type: 'text/plain'});
    if (textFile !== null) {
      window.URL.revokeObjectURL(textFile);
    }
    textFile = window.URL.createObjectURL(data);
    return textFile;
  };

  var create = document.getElementById('create'),
    textbox = document.getElementById('textbox');
  create.addEventListener('click', function () {
    var link = document.getElementById('downloadlink');
    link.href = makeTextFile(textbox.value);
    link.style.display = 'block';
  }, false);
})();

回答by Stephen Kaufman

Some things are possible, but not all, say for example you want line breaks, instead of using <br />use %0D%0A

有些事情是可能的,但不是全部,例如你想要换行,而不是使用<br />use%0D%0A

Example:

例子:

<a href="mailto:?subject=&body=Hello,%0D%0A%0D%0AHere is the link to the PDF Brochure.%0D%0A%0D%0ATo view the brochure please click the following link: http://www.uyslist.com/yachts/brochure.pdf"><img src="images/email.png" alt="EMail PDF Brochure" /></a>                        

回答by Andrew Ferrier

It is worth pointing out that on Safari on the iPhone, at least, inserting basic HTML tags such as <b>, <i>, and <img>(which ideally you shouldn't use in other circumstances anymore anyway, preferring CSS) into the body parameter in the mailto:does appear to work - they are honored within the email client. I haven't done exhaustive testing to see if this is supported by other mobile or desktop browser/email client combos. It's also dubious whether this is really standards-compliant. Might be useful if you are building for that platform, though.

这是值得指出的是,在Safari在iPhone,至少,将基本的HTML标记,如<b><i><img>(在理想情况下,你不应该在其他情况下使用了,无论如何,宁愿CSS)插入身体参数mailto:确实出现了工作 - 他们在电子邮件客户端中受到尊重。我还没有进行详尽的测试以查看其他移动或桌面浏览器/电子邮件客户端组合是否支持此功能。这是否真的符合标准也值得怀疑。不过,如果您正在为该平台构建,可能会很有用。

As other responses have noted, you should also use encodeURIComponent on the entire body before embedding it in the mailto:link.

正如其他回复所指出的,您还应该在整个正文中使用 encodeURIComponent,然后再将其嵌入mailto:链接中。

回答by Peter

Thunderbird supportshtml-body: mailto:[email protected]?subject=Me&html-body=<b>ME</b>

雷鸟支持html-bodymailto:[email protected]?subject=Me&html-body=<b>ME</b>

回答by Rajan Kashiyani

I have same issue regarding sharing html. I do below html work for me. Replace < with < & > with >.

我有关于共享 html 的同样问题。我为我做下面的 html 工作。将 < 替换为 < & > 替换为 >。

<a href="mailto:?subject=link Share&body=&lt;a href='www.google.com'&gt;google&lt;/a&gt;">Email</a>

<a href="mailto:?subject=link Share&body=&lt;a href='www.google.com'&gt;google&lt;/a&gt;">Email</a>

Note: It work only in ubuntu. It will not work in windows.

注意:它仅适用于 ubuntu。它不会在 Windows 中工作。

回答by Miguel Ballén

Anybody can try the following (mailto function only accepts plaintext but here i show how to use HTML innertext properties and how to add an anchor as mailto body params):

任何人都可以尝试以下操作(mailto 函数只接受纯文本,但在这里我展示了如何使用 HTML 内部文本属性以及如何添加锚点作为 mailto 正文参数):

//Create as many html elements you need.

const titleElement = document.createElement("DIV");
titleElement.innerHTML = this.shareInformation.title; // Just some string

//Here I create an <a> so I can use href property
const titleLinkElement = document.createElement("a");
titleLinkElement.href = this.shareInformation.link; // This is a url

...

...

let mail = document.createElement("a");

// Using es6 template literals add the html innerText property and anchor element created to mailto body parameter
mail.href = 
  `mailto:?subject=${titleElement.innerText}&body=${titleLinkElement}%0D%0A${abstractElement.innerText}`;
mail.click();

// Notice how I use ${titleLinkElement} that is an anchor element, so mailto uses its href and renders the url I needed