使用 Nodemailer 发送邮件时出错
Getting error in Mail send Using Nodemailer
我创建了一个项目并按照 Nodemailer/Gmail - What exactly is a refresh token and how do I get one? 的回答中所写的所有步骤进行操作,但我出错了。
我使用以下代码:
var smtpTransport = nodemailer.createTransport("SMTP", {
service: "Gmail",
connectionTimeout : "7000",
greetingTimeout : "7000",
auth: {
XOAuth2: {
user: "",
clientId: "",
clientSecret: "",
refreshToken: ""
}
}
});
var mailOptions = {
from: "",
to:usersEmailId,
subject: 'subject',
html: 'string Of Html'
}
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
smtpTransport.close();
});
出现以下错误
{ [Error: Connection timeout] code: 'ETIMEDOUT', errno: 'ETIMEDOUT', stage: 'init' }
{ [Error: Connection timeout] code: 'ETIMEDOUT', errno: 'ETIMEDOUT', stage: 'init' }
{ [XOAUTH2Error: invalid_client] name: 'XOAUTH2Error', stage: 'auth' }
{ [XOAUTH2Error: invalid_client] name: 'XOAUTH2Error', stage: 'auth' }
我的第二个问题是如何发送附件。我只有文件名和文件的 url
。
我围绕 Nodemailer/nodemailer-smtp-transport/xoauth2.
编写了一个包装器模块
看看这个Gist如果有帮助:
// USAGE
// response is coming (in my case) from REDIS
var constants = {
stmp_host: 'smtp-relay.gmail.com',
user: resp.SUPORTE_MAIL,
user_name: resp.SUPORTE_NAME,
clientId: resp.OAUTH_CLIENT_ID,
clientSecret: resp.OAUTH_CLIENT_SECRET,
refreshToken: resp.OAUTH_REFRESH_TOKEN
};
var Mailer = require('./my-mailer.js');
var mailer = new Mailer(constants);
mailer.createTransporter().then(function(){
var send_info = {
subject: 'A test!',
html: 'some<br>formatted <strong>text</strong>',
to_name: 'Some Name',
to_email: 'some@gmail.com'
};
return mailer.getMailObject(send_info);
}).then(function(mail_obj){
mailer.sendMail(mail_obj).then(function(info) {
console.info('sent to: ', info);
process.exit();
}).catch(function(err) {
console.info('error: ', err);
});
});
使用这个是因为 nodemailer 文档中的路径错误。这是 nodemailer 使用文件路径中的问题,这是有效的
attachments : [
{ // file on disk as an attachment
filename: 'name Of File',,
filePath : 'url of file' // stream this file
},
],
alternatives : [
{ // file on disk as an attachment
filename: 'name Of File',
filePath : 'url of file' // stream this file
},
],
我创建了一个项目并按照 Nodemailer/Gmail - What exactly is a refresh token and how do I get one? 的回答中所写的所有步骤进行操作,但我出错了。
我使用以下代码:
var smtpTransport = nodemailer.createTransport("SMTP", {
service: "Gmail",
connectionTimeout : "7000",
greetingTimeout : "7000",
auth: {
XOAuth2: {
user: "",
clientId: "",
clientSecret: "",
refreshToken: ""
}
}
});
var mailOptions = {
from: "",
to:usersEmailId,
subject: 'subject',
html: 'string Of Html'
}
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
smtpTransport.close();
});
出现以下错误
{ [Error: Connection timeout] code: 'ETIMEDOUT', errno: 'ETIMEDOUT', stage: 'init' }
{ [Error: Connection timeout] code: 'ETIMEDOUT', errno: 'ETIMEDOUT', stage: 'init' }
{ [XOAUTH2Error: invalid_client] name: 'XOAUTH2Error', stage: 'auth' }
{ [XOAUTH2Error: invalid_client] name: 'XOAUTH2Error', stage: 'auth' }
我的第二个问题是如何发送附件。我只有文件名和文件的 url
。
我围绕 Nodemailer/nodemailer-smtp-transport/xoauth2.
编写了一个包装器模块看看这个Gist如果有帮助:
// USAGE
// response is coming (in my case) from REDIS
var constants = {
stmp_host: 'smtp-relay.gmail.com',
user: resp.SUPORTE_MAIL,
user_name: resp.SUPORTE_NAME,
clientId: resp.OAUTH_CLIENT_ID,
clientSecret: resp.OAUTH_CLIENT_SECRET,
refreshToken: resp.OAUTH_REFRESH_TOKEN
};
var Mailer = require('./my-mailer.js');
var mailer = new Mailer(constants);
mailer.createTransporter().then(function(){
var send_info = {
subject: 'A test!',
html: 'some<br>formatted <strong>text</strong>',
to_name: 'Some Name',
to_email: 'some@gmail.com'
};
return mailer.getMailObject(send_info);
}).then(function(mail_obj){
mailer.sendMail(mail_obj).then(function(info) {
console.info('sent to: ', info);
process.exit();
}).catch(function(err) {
console.info('error: ', err);
});
});
使用这个是因为 nodemailer 文档中的路径错误。这是 nodemailer 使用文件路径中的问题,这是有效的
attachments : [
{ // file on disk as an attachment
filename: 'name Of File',,
filePath : 'url of file' // stream this file
},
],
alternatives : [
{ // file on disk as an attachment
filename: 'name Of File',
filePath : 'url of file' // stream this file
},
],