Nodemailer 无法在生产服务器上使用 aws 发送邮件
Nodemailer is not able to send mail using aws on production server
我在通过 nodemailer 和 AWS 发送邮件时遇到问题。以下是我的设置:
var transporter = nodemailer.createTransport("SMTP", smtpTransport({
host: 'email-smtp.us-west-2.amazonaws.com',
service: 'SES',
port: 25,
secure: true,
debug: true,
auth: {
user: 'XXX',
pass: 'XXX'
}
}));
邮件发送代码:
transporter.sendMail({
from: xxx@xxx.com,
to: xxx@xxx.com,
subject: 'XXX',
html: 'XXX'
}, function(error, response) {
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
});
我收到以下错误:
{ [Error: connect ECONNREFUSED 127.0.0.1:25]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 25,
stage: 'init' }
这是我打印的运输工具对象的样子:
Transport {
options:
SMTPTransport {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
options:
{ host: 'email-smtp.us-east-1.amazonaws.com',
service: 'SES',
port: 465,
secure: true,
debug: true,
auth: [Object] },
logger: { info: [Function], debug: [Function], error: [Function] },
name: 'SMTP',
version: '2.4.1[client:2.3.1]',
maxConnections: 5,
maxMessages: Infinity },
transportType: 'SMTP',
dkimOptions: false,
transport:
SMTPTransport {
options:
SMTPTransport {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
options: [Object],
logger: [Object],
name: 'SMTP',
version: '2.4.1[client:2.3.1]',
maxConnections: 5,
maxMessages: Infinity },
pool:
SMTPConnectionPool {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
port: 25,
host: 'localhost',
options: [Object],
_connectionsAvailable: [],
_connectionsInUse: [Object],
_messageQueue: [Object],
_idgen: 2 } },
version: '0.3.35',
sendMail: [Function] }
你可以清楚地看到它使用的是我定义的不同设置。
我找不到问题所在。任何帮助将不胜感激。
如果你想通过 aws 发送邮件,最好使用 aws-sdk
模块。用法如下
var aws = require('aws-sdk');
module.exports = function (to, from, sub, body) {
aws.config.update({
accessKeyId: 'accessKeyId',
secretAccessKey: 'secretAccessKey',
region: 'region'
});
// load AWS SES
var ses = new aws.SES({apiVersion: 'latest'});
// this sends the email
ses.sendEmail({
Source: from,
Destination: {
ToAddresses: to
},
Message: {
Subject: {
Data: sub
},
Body: {
Html: {
Data: body
}
}
}
}
, function (err, data) {
if (err) {
console.log(err);
} else {
console.log('Email sent:');
console.log(data);
}
});
};
希望对您有所帮助:)
我在通过 nodemailer 和 AWS 发送邮件时遇到问题。以下是我的设置:
var transporter = nodemailer.createTransport("SMTP", smtpTransport({
host: 'email-smtp.us-west-2.amazonaws.com',
service: 'SES',
port: 25,
secure: true,
debug: true,
auth: {
user: 'XXX',
pass: 'XXX'
}
}));
邮件发送代码:
transporter.sendMail({
from: xxx@xxx.com,
to: xxx@xxx.com,
subject: 'XXX',
html: 'XXX'
}, function(error, response) {
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
});
我收到以下错误:
{ [Error: connect ECONNREFUSED 127.0.0.1:25]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 25,
stage: 'init' }
这是我打印的运输工具对象的样子:
Transport {
options:
SMTPTransport {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
options:
{ host: 'email-smtp.us-east-1.amazonaws.com',
service: 'SES',
port: 465,
secure: true,
debug: true,
auth: [Object] },
logger: { info: [Function], debug: [Function], error: [Function] },
name: 'SMTP',
version: '2.4.1[client:2.3.1]',
maxConnections: 5,
maxMessages: Infinity },
transportType: 'SMTP',
dkimOptions: false,
transport:
SMTPTransport {
options:
SMTPTransport {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
options: [Object],
logger: [Object],
name: 'SMTP',
version: '2.4.1[client:2.3.1]',
maxConnections: 5,
maxMessages: Infinity },
pool:
SMTPConnectionPool {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
port: 25,
host: 'localhost',
options: [Object],
_connectionsAvailable: [],
_connectionsInUse: [Object],
_messageQueue: [Object],
_idgen: 2 } },
version: '0.3.35',
sendMail: [Function] }
你可以清楚地看到它使用的是我定义的不同设置。
我找不到问题所在。任何帮助将不胜感激。
如果你想通过 aws 发送邮件,最好使用 aws-sdk
模块。用法如下
var aws = require('aws-sdk');
module.exports = function (to, from, sub, body) {
aws.config.update({
accessKeyId: 'accessKeyId',
secretAccessKey: 'secretAccessKey',
region: 'region'
});
// load AWS SES
var ses = new aws.SES({apiVersion: 'latest'});
// this sends the email
ses.sendEmail({
Source: from,
Destination: {
ToAddresses: to
},
Message: {
Subject: {
Data: sub
},
Body: {
Html: {
Data: body
}
}
}
}
, function (err, data) {
if (err) {
console.log(err);
} else {
console.log('Email sent:');
console.log(data);
}
});
};
希望对您有所帮助:)