Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在firebase函数中使用gmail时出现NodeEmailer错误_Node.js_Firebase_Smtp_Google Cloud Functions_Nodemailer - Fatal编程技术网

Node.js 在firebase函数中使用gmail时出现NodeEmailer错误

Node.js 在firebase函数中使用gmail时出现NodeEmailer错误,node.js,firebase,smtp,google-cloud-functions,nodemailer,Node.js,Firebase,Smtp,Google Cloud Functions,Nodemailer,我正在使用firebase云函数。我已配置以下设置。虽然这在我的本地机器上运行得很好,但在服务器上运行时却给了我一个问题。我在互联网上尝试了无数的工作,但没有成功。这有什么问题 'use strict' const functions = require('firebase-functions'); var admin = require('firebase-admin'); const express = require('express'); const nodemailer = requi

我正在使用firebase云函数。我已配置以下设置。虽然这在我的本地机器上运行得很好,但在服务器上运行时却给了我一个问题。我在互联网上尝试了无数的工作,但没有成功。这有什么问题

'use strict'
const functions = require('firebase-functions');
var admin = require('firebase-admin');
const express = require('express');
const nodemailer = require('nodemailer');
const app = express()

var emailRecepient;
var userName;


const smtpTransport = nodemailer.createTransport({
service: "gmail",
host: 'smtp.gmail.com',
port: 587, // tried enabling and disabling these, but no luck
secure: false, // similar as above
auth: {
  user: '<emailid>',
  pass: '<password>'
    },
        tls: {
        rejectUnauthorized: false
    }
});

var mailOptions = {
  from: 'test <hello@example.com>',
  to: emailRecepient,
  subject: 'Welcome to test',
  text: 'welcome ' + userName + ". did you see the new things?"
};

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

exports.sendEmails = functions.database.ref('/users/{userID}/credentials').onCreate((snap, context) => {

  const userID = context.params.userID;
  const vals = snap.val()

  userName = vals.name;
  emailRecepient = vals.email;

  smtpTransport.sendMail(mailOptions, function (error, info) {
  if (error) {
      console.log("Error sending email ---- ",error);
  }
  else {
      console.log('Email sent: ' + info.response);
  }
  });

  return true;

});

Cheers

当您在后台触发的云函数中执行异步操作时,必须返回一个承诺,以这种方式,云函数将等待该承诺解析以终止

这在Firebase官方视频系列中得到了很好的解释:。特别是看三个名为“学习JavaScript承诺”的视频(第2部分和第3部分特别关注后台触发的云函数,但之前确实值得看第1部分)

因此,您应该按如下方式修改代码:

exports.sendEmails = functions.database.ref('/users/{userID}/credentials').onCreate((snap, context) => {

  const userID = context.params.userID;
  const vals = snap.val()

  userName = vals.name;
  emailRecepient = vals.email;

  return smtpTransport.sendMail(mailOptions);
});
  return smtpTransport.sendMail(mailOptions)
    .then((info) => console.log('Email sent: ' + info.response))
    .catch((error) => console.log("Error sending email ---- ", error));
});
如果要将电子邮件发送的结果打印到控制台,可以执行以下操作:

exports.sendEmails = functions.database.ref('/users/{userID}/credentials').onCreate((snap, context) => {

  const userID = context.params.userID;
  const vals = snap.val()

  userName = vals.name;
  emailRecepient = vals.email;

  return smtpTransport.sendMail(mailOptions);
});
  return smtpTransport.sendMail(mailOptions)
    .then((info) => console.log('Email sent: ' + info.response))
    .catch((error) => console.log("Error sending email ---- ", error));
});


实际上,有一个官方的云函数示例正是这样做的,请参见

我看到这是一个很长的讨论,让我分享一些代码片段,这些代码片段对我来说适用于其他有类似问题的人,这样更容易理解

1) function.ts(它是用打字脚本编写的)


除此之外,不要忘记启用上的。也可能会有帮助。

在检查了上面列出的所有事情是否都在我的代码中完成后,我解决了登录Google帐户的问题(使用我在proyect中使用的帐户)。我必须从另一个来源(谷歌检测到Firebase试图访问该帐户)识别该活动,就是这样。
在那之后,我尝试从firebase cloud发送另一封电子邮件,它工作正常

,结果发现gmail安全规则需要24-48小时才能生效。我想,在那之后我就能知道了。我甚至收到了firebase团队的来信,但都没有用。我检查他们过去提出的问题。有人建议你等一等。所以是的。我会通知你的。谢谢你的反馈。仅供参考,使用Sendgrid效果很好。一个月有12000封邮件是免费的。请看“讨论”(即评论)回复:嘿,只是一个快速更新。它现在运行得很好!虽然我选择sendgrid只是为了节省邮件模板制作的时间。为帮助干杯。@AshishYadav为迟到的回复感到抱歉,伙计!我必须从所有帐户注销,并从其他浏览器登录。只需再次遵守所有协议。我不知道为什么,但那是从我开始的。连Firebase团队都回答了这个问题。甚至可以尝试清除缓存或历史记录
import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
import * as nodemailer from 'nodemailer';

admin.initializeApp();

// I'm taking all these constants as secrets injected dynamically (important when you make `git push`), but you can simply make it as a plaintext.
declare const MAIL_ACCOUNT: string; // Declare mail account secret.
declare const MAIL_HOST: string; // Declare mail account secret.
declare const MAIL_PASSWORD: string; // Declare mail password secret.
declare const MAIL_PORT: number; // Declare mail password secret.

const mailTransport = nodemailer.createTransport({
  host: MAIL_HOST,
  port: MAIL_PORT, // This must be a number, important! Don't make this as a string!!
  auth: {
    user: MAIL_ACCOUNT,
    pass: MAIL_PASSWORD
  }
});

exports.sendMail = functions.https.onRequest(() => {
  const mailOptions = {
    from: 'ME <SENDER@gmail.com>',
    to: 'RECEIVER@gmail.com',
    subject: `Information Request from ME`,
    html: '<h1>Test</h1>'
  };
  mailTransport
    .sendMail(mailOptions)
    .then(() => {
      return console.log('Mail sent'); // This log will be shown in Firebase Firebase Cloud Functions logs.
    })
    .catch(error => {
      return console.log('Error: ', error); // This error will be shown in Firebase Cloud Functions logs.
    });
});
const mailTransport = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: MAIL_ACCOUNT,
    pass: MAIL_PASSWORD
  }
});