nodemailor不能跨域发邮件

Nodemailor can't sending emails on cross domains

我正在使用 nodemailor 通过 node 和 express 应用程序发送电子邮件。

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

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 使用了上面的配置。谢谢

经过大量挖掘,我终于得到了以下答案,如果有人遇到同样的问题,请尝试以下解决方案。我添加了以下配置。

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 --save 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
        }
    }));
  • 上面添加了“名称”属性这是一个服务器名称,谢谢