自动打开默认的电子邮件客户端和预填充内容

我需要自动打开用户的默认电子邮件客户端时,他们保存一些内容的网页。我需要填充电子邮件主题,地址,并把一些内容的电子邮件正文。

实现这一目标的最佳选择是什么?

我知道 mailto:属性,但是用户必须点击它,我不确定它是否允许你指定主题和内容?

159150 次浏览

As described by RFC 6068, mailto allows you to specify subject and body, as well as cc fields. For example:

mailto:username@example.com?subject=Subject&body=message%20goes%20here

User doesn't need to click a link if you force it to be opened with JavaScript

window.location.href = "mailto:user@example.com?subject=Subject&body=message%20goes%20here";

Be aware that there is no single, standard way in which browsers/email clients handle mailto links (e.g. subject and body fields may be discarded without a warning). Also there is a risk that popup and ad blockers, anti-virus software etc. may silently block forced opening of mailto links.

JQuery:

$(function () {
$('.SendEmail').click(function (event) {
var email = 'sample@gmail.com';
var subject = 'Test';
var emailBody = 'Hi Sample,';
var attach = 'path';
document.location = "mailto:"+email+"?subject="+subject+"&body="+emailBody+
"?attach="+attach;
});
});

HTML:

 <button class="SendEmail">Send Email</button>

Implemented this way without using Jquery:

<button class="emailReplyButton" onClick="sendEmail(message)">Reply</button>


sendEmail(message) {
var email = message.emailId;
var subject = message.subject;
var emailBody = 'Hi '+message.from;
document.location = "mailto:"+email+"?subject="+subject+"&body="+emailBody;
}

Try this: It will open the default mail directly.

<a href="mailto:demo@demo.com"><img src="ICON2.png"></a>