MailTo与HTML正文

我的问题是这样的:

客户端收到由Exchange Server发送的电子邮件。 在邮件中,他有一个格式化的HTML与一些链接

href='mailto:etc...' 

我的问题是:我可以插入HTML格式的身体在mailto:部分的HREF?

就像是

<a href='mailto:me@me.com?subject=Me&body=<b>ME</b>'>Mail me</a>


请注意(2016)在iOS中,为简单的斜体,粗体格式添加<i><b>标签是完全正确的。

正如你在RFC 6068中看到的,这是不可能的:

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

不,这是不可能的。

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

如果你能够使用JavaScript,那么“encodeURIComponent()”可能会像下面一样使用…

 var formattedBody = "FirstLine \n Second Line \n Third Line"; var mailToLink = "mailto:x@y.com?body=" + encodeURIComponent(formattedBody); window.location.href = mailToLink; 

我已经使用了这个,它似乎与Outlook的工作,而不是使用HTML,但你可以格式文本换行符,至less当主体被添加为输出。

 <a href="mailto:email@address.com?subject=Hello world&body=Line one%0DLine two">Email me</a> 

值得指出的是,在iPhone的Safari上,至less要插入基本的HTML标签,如<b><i><img> (理想情况下,不应该在其他情况下使用,偏好CSS)到mailto:的body参数中mailto:确实似乎可以工作 – 它们在电子邮件客户端中得到遵守。 我还没有做过彻底的testing,看看是否支持其他移动或桌面浏览器/电子邮件客户端组合。 这是否真的符合标准也是可疑的。 但是,如果你正在为这个平台build设,可能会有用。

正如其他响应已经注意到的,你也应该在整个主体上使用encodeURIComponent,然后将其embedded到mailto:链接中。

有些事情是可能的,但不是全部,例如说你想换行,而不是使用<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> 

这不是你想要的,但是可以使用现代JavaScript在客户端上创build一个EML文件,并将其传输到用户的文件系统,这将在其邮件程序(如Outlook)中打开一个包含HTML的丰富电子邮件:

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

这是一个包含图像和表格的电子邮件: 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.comhttp://img.dovov.comlogo/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); })(); 

可以inputunicode值来插入换行符(即:'\ u0009'),但HTML标签有不同程度的支持,应该避免。

我这样工作:

 var newLine = escape("\n"); var body = "Hello" + newLine +"World"; 

输出将是:

 Hello World 

以下是将所有内容添加到MAILTO链接的方法:

 <a href="mailto:YourName@YourSite.com? cc=someone@YourSite.com&bcc=someoneElse@YourSite.com &subject=Shipping%20Information%20Request&body=Please%20tell%20me%20if%20my%20order%20has%20shipped!">Shipping Request</a> 

每个组件都由&符号(&)分隔。 只有在初始邮件地址之后的第一个组件在和号之前有一个问号(?)。

URL编码是关键! 所以对于你身体的例子,而不是你的

 href='mailto:me@me.com?subject=Me&body=<b>ME</b>' 

…你可以尝试:

 href='mailto:me@me.com?subject=Me&body=%3cb%3eME%3c%2fb%3e' 

这是另一条可能尝试的路线。 创build一个javascript函数来打开一个ActiveX对象。 这有一个不幸的限制,只能在IE和Outlook工作,并可能导致您的网页显示ActiveX的警告。 但是,如果你能忍受这些警告,它就能胜任。 这里有一个可以从中得出的工作示例:

 <html> <head> <script type='text/javascript' language='javascript'> function OpenOutlookNewEmail() { try { var outlookApp = new ActiveXObject("Outlook.Application"); var nameSpace = outlookApp.getNameSpace("MAPI"); mailFolder = nameSpace.getDefaultFolder(6); mailItem = mailFolder.Items.add('IPM.Note.FormA'); mailItem.Subject = "Me"; mailItem.To = "me@me.com"; mailItem.HTMLBody = "<b>ME</b>"; mailItem.display(0); } catch (e) { alert(e); // act on any error that you get } } </script> </head> <body> <a href='javascript:OpenOutlookNewEmail()' >email</a> </body> </html>