使用Nodemailer从本地主机发送电子邮件
问题描述:
我想在本地服务器上发送邮件,但它似乎不适用于Nodemailer和NodeJS。使用Nodemailer从本地主机发送电子邮件
有没有解决办法从本地发送邮件?
var contact = {subject: 'test', message: "test message", email: '[email protected]'};
var to = "[email protected]";
var transporter = nodemailer.createTransport();
transporter.sendMail({
from: contact.email,
to: to,
subject: contact.subject,
text: contact.message
});
答
不应该是从本地服务器运行的问题。
看起来你正在使用的直接运输,这是在文档中描述:
...not reliable as outgoing port 25 used is often blocked by default. Additionally mail sent from dynamic addresses is often flagged as spam. You should really consider using a SMTP provider.
一种解决方案是定义一个SMTP传输:
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'password'
}
});
还有其他的解决方案here,如使用传输插件。
无论如何,当您不向sendMail函数添加回调时,诊断问题非常困难。当您发送邮件,像这样做,然后你可以检查控制台来查看错误可能是什么:
transporter.sendMail({
from: contact.email,
to: to,
subject: contact.subject,
text: contact.message
}, function(error, info){
if(error){
console.log(error);
}else{
console.log('Message sent: ' + info.response);
}
});
答
其他什么@cheniel说:确保“从”电子邮件是相同的用户电子邮件作为转运对象中的电子邮件。并且“to”必须是有效的电子邮件并存在(检查值是否设置正确(未定义且不为空))。
答
var nodemailer = require('nodemailer');
// Create a SMTP transport object
var transport = nodemailer.createTransport("SMTP", {
service: 'Hotmail',
auth: {
user: "username",
pass: "password"
}
});
// Message object
var message = {
// sender info
from: '[email protected]',
// Comma separated list of recipients
to: req.query.to ,
// Subject of the message
subject:req.query.subject //'Nodemailer is unicode friendly ✔',
// plaintext body
text: req.query.text //'Hello to myself!',
// HTML body
/* html:'<p><b>Hello</b> to myself <img src="cid:[email protected]"/></p>'+
'<p>Here\'s a nyan cat for you as an embedded attachment:<br/></p>'*/
};
console.log('Sending Mail');
transport.sendMail(message, function(error){
if(error){
console.log('Error occured');
console.log(error.message);
return;
}
console.log('Message sent successfully!');
// if you don't want to use this transport object anymore, uncomment
//transport.close(); // close the connection pool
});
您必须指定类似于SMTP的传输协议或创建自己的传输。您没有在您的代码中指定此传输协议。
它不工作太多.. {[错误:没有收件人定义]代码:'EENVELOPE'}' – tonymx227
任何解决方案,该错误代码有史以来? –