Nodemailor无法跨域发送电子邮件

时间:2019-05-24 08:52:32

标签: node.js smtp nodemailer email-integration

我正在使用nodemailor来发送带有node和express应用程序的电子邮件。

问题:当我在跨域上发送电子邮件时,例如:我正在使用hostgator电子邮件服务器时,它仅通过hostgator在托管电子邮件中发送电子邮件,但是当我在Gmail上发送电子邮件时,它将返回成功

250 OK id=1hU5l2-000m0C-Lh 

但没有收到电子邮件。

注意:获得成功响应但在跨域的情况下不会收到电子邮件

const nodemailer = require('nodemailer');
const keys = require('../config/keys');
const smtpTransport = require('nodemailer-smtp-transport');
emailCredentaials = (data) => {

      var transporter = nodemailer.createTransport(smtpTransport ({
        host: 'my.hostgator.com',
        port: 465,
        secure:true,
        auth: {
            user: keys.email.emailUserName,
            pass: keys.email.emailPassword
          },
          tls: {
            // do not fail on invalid certs
            rejectUnauthorized: false
          }  
        }));
    var mailOptions = {
        from: keys.email.emailUserName,
        to: data.email,
        subject: 'Demo account credentials',
        html: '<h3>Please Follow the link to login : '
            + keys.emailLinks.accountCredentailLink + '<h3>' + '<br>Useraname &nbsp;&nbsp; : &nbsp;&nbsp;' + data.email + '</br><br> Password &nbsp;&nbsp; : &nbsp;&nbsp;' + data.password + '</br>'
    };

    transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
            console.log("***********" + error);
        } else {
            console.log('*********Email sent:********' + info.response);
        }
    });
}
module.exports = emailCredentaials;

我对nodemailor使用了以上配置。谢谢

1 个答案:

答案 0 :(得分:-1)

经过大量研究后,我再次返回以下答案,如果有人遇到相同的问题,请尝试以下解决方案。我添加了以下配置。

const nodemailer = require('nodemailer');
const keys = require('../config/keys');
const smtpTransport = require('nodemailer-smtp-transport');

emailCredentaials = (data) => {

    var transporter = nodemailer.createTransport(smtpTransport({
        name: 'hostgator',
        host: 'my.hostgator.com',
        port: 465,
        secure: true,
        auth: {
            user: keys.email.emailUserName,
            pass: keys.email.emailPassword
        }
    }));

    var mailOptions = {
        from: keys.email.emailUserName,
        to: data.email,
        subject: 'Demo account credentials',
        html: '<h3>Please Follow the link to login : '
            + keys.emailLinks.accountCredentailLink + '<h3>' + '<br>Useraname &nbsp;&nbsp; : &nbsp;&nbsp;' + data.email + '</br><br> Password &nbsp;&nbsp; : &nbsp;&nbsp;' + data.password + '</br>'
    };


    transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
            console.log("***********" + error);
        } else {
            console.log('*********Email sent:********' + info.response);
        }
    });
}
module.exports = emailCredentaials;

发布的问题中的更改:

  • 需要添加“ nodemailer-smtp-transport”
  

npm install-保存nodemailer-smtp-transport

var transporter = nodemailer.createTransport(smtpTransport({
        name: 'hostgator',
        host: 'my.hostgator.com',
        port: 465,
        secure: true,
        auth: {
            user: keys.email.emailUserName,
            pass: keys.email.emailPassword
        }
    }));
  • 在上面添加了“ 名称”属性,它是服务器名称,谢谢