Google 在 firebase 函数中发送 gmail 时阻止登录

Google blocks login when sending gmail in firebase functions

我正在尝试使用 node 和 nodemailer 在 firebase 函数中发送电子邮件,但 google 总是将此邮件发送给我。

这是我的代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const nodemailer = require('nodemailer');

admin.initializeApp();

const transporter = nodemailer.createTransport({
  service: 'gmail',
  host: 'smtp.gmail.com',
  port: 465,
  secure: true,
  auth: {
    user: '*****@gmail.com',
    pass: '******',
  },
});

exports.registerCompany = functions.https.onCall((data, context) => {
  return new Promise((resolve, reject) => { 
    //do somethings
    const mailOptions = {
      from: 'marcokse24@gmail.com',
      to: 'marcokse@hotmail.es',
      subject: 'New Register Company',
      text: `Company: ${company.name} \nUser: ${user.email}`,
    };

    transporter.sendMail(mailOptions, function (error, info) {
      if (error) {
         console.log(error);
       } else {
         console.log('Email sent: ' + info.response);
       }
    });
  }
}

当我 运行 它在本地主机上时,我没有收到来自 Google 的警报或阻止。

这是出于安全原因,不是您的代码或 Firebase 函数的问题。

当您在 PC 中使用您的代码时,Google 已识别出您的 PC 是受信任的设备。当您 运行 您的代码位于其他位置、服务器等时,出于安全原因,Gmail 将阻止登录,因为不知道这是您还是其他人。

那么需要给Gmail添加一个权限,允许应用使用你的账号登录。

This answer 可以帮助您:

Head over to Account Security Settings (https://www.google.com/settings/security/lesssecureapps) and enable "Access for less secure apps", this allows you to use the google smtp for clients other than the official ones.

Update

Google has been so kind as to list all the potential problems and fixes for us. Although I recommend trying the less secure apps setting. Be sure you are applying these to the correct account.

  • If you've turned on 2-Step Verification for your account, you might need to enter an App password instead of your regular password.
  • Sign in to your account from the web version of Gmail at https://mail.google.com. Once you’re signed in, try signing in to the mail app again.
  • Visit http://www.google.com/accounts/DisplayUnlockCaptcha and sign in with your Gmail username and password. If asked, enter the letters in the distorted picture.
  • Your app might not support the latest security standards. Try changing a few settings to allow less secure apps access to your account.
  • Make sure your mail app isn't set to check for new email too often. If your mail app checks for new messages more than once every 10 minutes, the app’s access to your account could be blocked.

您可能还想更改端口,从 465 更改为 587