您只能从前端使用 smtp 服务器触发邮件,例如 smtpJS
接下来的步骤是
- 转到此链接smtpJS
- 点击GetSMTPCredentials并注册。
- 注册后,记下凭据并单击ENCRYPTSMTPCrendentials
示例如下
var test={
sendEmail(subject,to,body){
/* SmtpJS.com - v3.0.0 */
let Email = { send: function (a) { return new Promise(function (n, e) { a.nocache = Math.floor(1e6 * Math.random() + 1), a.Action = "Send"; var t = JSON.stringify(a); Email.ajaxPost("https://smtpjs.com/v3/smtpjs.aspx?", t, function (e) { n(e) }) }) }, ajaxPost: function (e, n, t) { var a = Email.createCORSRequest("POST", e); a.setRequestHeader("Content-type", "application/x-www-form-urlencoded"), a.onload = function () { var e = a.responseText; null != t && t(e) }, a.send(n) }, ajax: function (e, n) { var t = Email.createCORSRequest("GET", e); t.onload = function () { var e = t.responseText; null != n && n(e) }, t.send() }, createCORSRequest: function (e, n) { var t = new XMLHttpRequest; return "withCredentials" in t ? t.open(e, n, !0) : "undefined" != typeof XDomainRequest ? (t = new XDomainRequest).open(e, n) : t = null, t } };
Email.send({
SecureToken : process.env.SECURE_TOKEN, // write your secure token
To : to, // to include multiple emails you need to mention an array
From : process.env.EMAIL_HOST,
Subject : subject,
Body : body
})
.then(message=>{
// alert(message);
});
}
}
export default test;
你可以像下面这样导入上面的js文件并调用方法
import test from '../../components/sendEmail'
test.sendEmail("sub","msg");