【问题标题】:Gmail with Heroku to send mail使用 Heroku 发送邮件的 Gmail
【发布时间】:2021-06-06 06:20:11
【问题描述】:

我想添加到我的网络应用程序,以便在订购后发送邮件。

我选择 Nodemailer 是因为它是最著名的 npm。

我编写了我的请求并在本地环境中运行。

我将代码上传到 Heroku,但出现错误。

错误:无效登录:534-5.7.14

我检查了人们告诉我禁用验证码的愿望:UnlockCaptcha

现在我仍然收到同样的错误,我收到一封邮件说 google 阻止了连接我该怎么办?

const nodemailer = require('nodemailer');
const { sendLog } = require('../middleware/sendLog');
const { coupons, actions } = require('../constant/actionCoupon');

var simple = function () {
  var textMultiple = {
    text1: 'text1',
    text2: 'text2',
  };
  return textMultiple;
};
// send mail system for the (REQUEST ACCEPTED SYSTEM)
const sendMail = (mail, action) => {
  let mailTransporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
      user: process.env.MAIL,
      pass: process.env.PASSWORD,
    },
  });

  let mailDetails = {
    from: process.env.MAIL,
    to: mail,
    subject: `Thank you for your purchase. with love FameGoal`,
    text: "for any probleme please reply on this message",
  };

  mailTransporter.sendMail(mailDetails, function (err, data) {
    if (err) {
      console.log(err);
      console.log(`error sent mail to ${mail}`, 'error');
    } else {
      console.log('succeed');
      console.log(`succesfully sent mail to ${mail}`, 'info');
    }
  });
};

exports.sendMail = sendMail;

【问题讨论】:

标签: javascript node.js reactjs express nodemailer


【解决方案1】:

使用 Gmail 作为 SMTP 中继并不是最理想的,因为 Google 服务器有时可能会拒绝基本的用户名/密码身份验证。

有一些解决方法。最理想的方式是使用 OAuth2 发送邮件。

OAuth2

OAuth2 使用访问令牌而不是密码来执行身份验证。 我不会详细介绍设置 OAuth2 的步骤,因为它可能需要一些时间,但如果您有兴趣,这个答案:https://stackoverflow.com/a/51933602/10237430 会介绍所有步骤。

应用密码

如果您尝试发送电子邮件的 Google 帐户启用了两步验证,则无法使用密码发送电子邮件。相反,您需要在 Google 网站上生成一个应用专用密码并将其传递到密码字段中。

更多信息请点击此处:https://support.google.com/accounts/answer/185833?hl=en

启用安全性较低的应用

如果您仍想使用当前设置,则必须确保在您发送电子邮件的 Google 帐户上启用安全性较低的应用。这将让您仅使用电子邮件和密码即可通过 Google 进行身份验证。

更多信息在这里:https://support.google.com/accounts/answer/6010255?hl=en

在您启用安全性较低的应用之前,基本密码身份验证将不起作用。

【讨论】:

    猜你喜欢
    • 2016-07-14
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 2011-11-24
    • 2018-05-08
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    相关资源
    最近更新 更多