第一次使用節點並使用電子郵件配置。我下載了這個application from here,它工作(使用mustache.js爲電子郵件生成模板),但測試電子郵件以我的Gmail垃圾郵件結束。通過nodemailer從我的服務器發送的電子郵件通過垃圾郵件發送:(未知發件人)
from: via vps1234.inmotionhosting.com
to: [email protected]
date: Tue, Aug 8, 2017 at 5:30 PM
subject: Thanks! and review your experience
mailed-by: vps1234.inmotionhosting.com
security: Standard encryption (TLS) Learn more
-
var nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
service: ' ',
secure: false,
port: 25,
auth: {
user: '[email protected]',
pass: 'password1234'
},
tls: {
rejectUnauthorized: false,
}
}),
EmailTemplate = require('email-templates').EmailTemplate,
path = require('path'),
Promise = require('bluebird');
let users = [
{
name: 'John',
note: 'I found your purse',
email: '[email protected]',
}
];
function sendEmail (obj) {
return transporter.sendMail(obj);
}
function loadTemplate (templateName, contexts) {
let template = new EmailTemplate(path.join(__dirname, 'templates', templateName));
return Promise.all(contexts.map((context) => {
return new Promise((resolve, reject) => {
template.render(context, (err, result) => {
if (err) reject(err);
else resolve({
email: result,
context,
});
});
});
}));
}
loadTemplate('welcome', users).then((results) => {
return Promise.all(results.map((result) => {
sendEmail({
to: result.context.email,
from: 'Me :)',
subject: result.email.subject,
html: result.email.html,
text: result.email.text,
});
}));
}).then(() => {
console.log('Yay!');
});
這是Nodemailer樣板,這也是我測試了我的服務器上,而它的工作正常,和電子郵件沒有得到標記:
var nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
service: ' ',
secure: false,
port: 25,
auth:{
user: '[email protected]',
pass: 'password1234'
},
tls: {
rejectUnauthorized: false,
}
});
let helperOptions = {
from: '<[email protected]>',
to: '[email protected]',
};
transporter.sendMail(helperOptions, (error, info) =>{
if(error){return alert(error);}
console.log("sent" . info);
})
一般來說,從您自己的Web服務器發送電子郵件是一個壞主意,你看過Amazon SES或Mailgun嗎?這些服務將提供SMTP端點來發送電子郵件(並且它們完成所有繁重的工作,而不會被標記爲垃圾郵件) – jye265