我在HTML文档中有几个mailto
链接。
<a href="mailto:etc...">
我能否在 href
的 mailto:
部分插入带有 HTML 格式的正文内容?
<a href="mailto:me@me.com?subject=Me&body=<b>ME</b>">Mail me</a>
请注意,在iOS中(2016年),添加<i>
和<b>
标签以进行简单的斜体、粗体格式化是完全可以的。
我在HTML文档中有几个mailto
链接。
<a href="mailto:etc...">
我能否在 href
的 mailto:
部分插入带有 HTML 格式的正文内容?
<a href="mailto:me@me.com?subject=Me&body=<b>ME</b>">Mail me</a>
请注意,在iOS中(2016年),添加<i>
和<b>
标签以进行简单的斜体、粗体格式化是完全可以的。
虽然不能使用HTML格式化电子邮件正文,但可以像之前建议的那样添加换行符。
如果您能够使用JavaScript,那么"encodeURIComponent()"可能会有用,如以下代码所示...
var formattedBody = "FirstLine \n Second Line \n Third Line";
var mailToLink = "mailto:x@y.com?body=" + encodeURIComponent(formattedBody);
window.location.href = mailToLink;
raw(“text \n more text \n\n\t”)
函数来封装文本,并将其转换为电子邮件正文中的换行符和制表符。 - FireDragon不可能实现。
虽然不完全符合您的要求,但使用现代JavaScript可以在客户端创建一个EML文件并将其流式传输到用户的文件系统中,这应该会在他们的邮件程序(例如Outlook)中打开包含HTML的丰富电子邮件:
https://dev59.com/oV4c5IYBdhLWcg3wapvz#27971771
这里是一个包含图像和表格的电子邮件jsfiddle:https://jsfiddle.net/seanodotcom/yd1n8Lfh/
HTML
<!-- https://jsfiddle.net/seanodotcom/yd1n8Lfh -->
<textarea id="textbox" style="width: 300px; height: 600px;">
To: User <user@domain.demo>
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>$4395.00</td>
</tr>
<tr>
<td>OU812</td>
<td>9</td>
<td>$4395.00</td>
</tr>
<tr>
<td>OU812</td>
<td>9</td>
<td>$4395.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
(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);
})();
<a href="mailto:email@address.com?subject=Hello world&body=Line one%0DLine two">Email me</a>
有些事情是可能的,但并非全部。例如,您想要换行,而不是使用<br />
,可以使用%0D%0A
示例:
<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>
<b>
、<i>
和<img>
)插入到mailto:
的body参数中时(在其他情况下最好不要再使用它们,而是使用CSS),它们会在电子邮件客户端中被正确显示。我没有进行详尽的测试以查看其他移动或桌面浏览器/电子邮件客户端组合是否支持此功能。这也值得怀疑是否真的符合标准规范,但如果您正在为该平台构建应用程序,这可能会很有用。mailto:
链接之前,您还应该对其进行encodeURIComponent处理。Thunderbird支持 html-body
: mailto:me@me.com?subject=我&html-body=<b>我</b>
虽然在URL的参数范围内可能无法实现,但有一个巧妙的解决方案可以允许完整的HTML。这个概念是在页面上有一个隐藏的元素(我在下面的示例中使用了Bootstrap和Jquery),它被暂时显示出来并复制HTML(如此处所述:如何从div复制文本到剪贴板)。之后,您将用户重定向到邮件链接,因此实际上他们只需要在指定的邮件程序中点击“粘贴”即可。我只在Linux / Thunderbird上测试过这个方法,但粘贴也适用于Gmail网页。
<div id="copyEmailText" class="d-none"><p><strong>This is some HTML</strong>. Please hit paste when your email program opens.</p>
function copyDivToClipboard(element) {
var range = document.createRange();
range.selectNode(element);
window.getSelection().removeAllRanges(); // clear current selection
window.getSelection().addRange(range); // to select text
document.execCommand('copy');
window.getSelection().removeAllRanges();// to deselect
}
$('#copyEmail').on('click',function(){
$('#copyEmailText').toggleClass('d-none');
copyDivToClipboard($('#copyEmailText')[0]);
window.location.href = 'mailto:?subject=Email subject text';
$('#copyEmailText').toggleClass('d-none');
})
//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