nodemailer不能在本地主机或云上工作?

时间:2018-07-04 17:02:56

标签: javascript node.js nodemailer

我尝试使用nodemailer来通过我的本地系统以及在heroku应用程序上发送邮件,但是在两种情况下,我都会在这里超时,这是我的代码,现在我已经尝试使用gmail和smtp电子邮件了,但还是没有可以

([^ymd])(?!.*\1)

请遍历代码,让我知道我可以进行哪些更改以使其正常工作。

2 个答案:

答案 0 :(得分:0)

很可能您实际上未在Heroku或本地主机上运行SMTP服务器,nodemailer不会为您执行此操作。您可以使用Google免费的smtp服务器,如此处https://www.digitalocean.com/community/tutorials/how-to-use-google-s-smtp-server所述,它将传输您的邮件。

let transporter = nodemailer.createTransport({
        host: 'smtp.gmail.com',
        port: 465,
        secure: true, // true for 465, false for other ports
        auth: {
            user: account.user, // generated ethereal user
            pass: account.pass // generated ethereal password
        }
    });

将您的运输工具更改为上述内容,并提供gmail登录详细信息,然后在邮件选项中提供与您相同的详细信息,如选项中所述,它将从chiragunplugged@chiragunplugged.com中显示出来。

答案 1 :(得分:0)

在本地主机中,它不起作用,因为发送电子邮件需要安全的连接,但是使用gmail [smtp(简单邮件传输协议)],我们可以实现此功能。

别忘了先进行设置-Allow less secure apps to access account

已授予其访问Gmail帐户的权限。

默认情况下,此设置为关闭,而您只需将其打开。现在继续编码部分。

///////////////////// --------------------------- --------------------------------------- //////////// ///////////////////////

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
requireTLS: true,
auth: {
    user: 'your.gmail.account@gmail.com', // like : abc@gmail.com
    pass: 'your.gmailpassword'           // like : pass@123
}
});

let mailOptions = {
 from: 'your.gmail.account@gmail.com',
 to: 'receivers.email@domain.com',
 subject: 'Check Mail',
 text: 'Its working node mailer'
};

transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
     return console.log(error.message);
  }
console.log('success');
});