使用Cloud Functions在Firestore上创建用户时发送电子邮件

时间:2019-03-29 23:38:20

标签: firebase google-cloud-functions

在flutter应用程序上创建用户后,我尝试发送电子邮件验证链接,但未发送电子邮件,并且在Cloud Functions日志中,我在部署时收到消息:

  

{“ @ type”:“ type.googleapis.com/google.cloud.audit.AuditLog”,“状态”:{“代码”:9,“消息”:“ FAILED_PRECONDITION”},“ authenticationInfo”:{ “ principalEmail”:“ *************”},“ requestMetadata”:{“ callerIp”:“ 186.216.140.62”,“ callerSuppliedUserAgent”:“ FirebaseCLI / 6.5.0,gzip(gfe ),gzip(gfe)“,” requestAttributes“:{” time“:” 2019-03-29T23:21:10.130Z“,” auth“:{}},” destinationAttributes“:{}},” serviceName“: “ cloudfunctions.googleapis.com”,“ methodName”:“ google.cloud.functions.v1.CloudFunctionsService.UpdateFunction”,“ authorizationInfo”:[{“ permission”:“ cloudfunctions.functions.update”,“ granted”:true, “ resourceAttributes”:{}},{“ resource”:“ projects / pppppp-9800a / locations / us-central1 / functions / sendVerificationEmail”,“ permission”:“ cloudfunctions.functions.update”,“ granted”:true,“ resourceAttributes“:{}}],” resourceName“:” projects / pppppp-9800a / locations / us-central1 / functions / sendVerificationEmail“,” request“:{” @ type“:” type.googleapis.com/google.cloud .functions.v1.UpdateFunctionRequest“,” function“:{” labels“:{” deploym ent-tool“:” cli-firebase“},” eventTrigger“:{” eventType“:” providers / cloud.firestore / eventTypes / document.create“,” resource“:” projects / pppppp-9800a / databases /(默认)/ documents / users / {userId}“,”服务“:” firestore.googleapis.com“},” sourceUploadUrl“:” https://storage.googleapis.com/gcf-upload-us-central1-dc1829cf-3a07-4951-be81-1a15f892ed8d/8ea3f162-c860-4846-9064-04a855efca2f.zip?GoogleAccessId=service-73683634264@gcf-admin-robot.iam.gserviceaccount.com&Expires=1553903464&Signature= **************** **“,”名称“:” projects / pppppp-9800a / locations / us-central1 / functions / sendVerificationEmail“}}}

我的代码:

exports.sendVerificationEmail = functions.firestore.document('users/{userId}').onCreate((snap, context) => {
  const user = snap.data();
  console.log("----------------------");
  console.log("user created: " + user.uidColumn);
  admin.auth().generateEmailVerificationLink(user.email).then((link) => {
    console.log("**********" + link);
    sendVerificationEmail(user.emailColumn, link);
    return 0;
  }).catch(e => {
    console.log(e);
  })
  return 0;
});

function sendVerificationEmail(email, link) {
  var smtpConfig = {
    host: 'smtp.gmail.com',
    port: 465,
    secure: true, // use SSL
    auth: {
      user: 'myappemail@gmail.com',
      pass: 'password'
    }
  };

  var transporter = nodemailer.createTransport(smtpConfig);

  var mailOptions = {
    from: "qeaapp@gmail.com", // sender address
    to: email, // list of receivers
    subject: "Email verification", // Subject line
    text: "Email verification, press here to verify your email: " + link,
    html: "<b>Hello there,<br> click <a href=" + link + "> here to verify</a></b>" // html body
  };

  transporter.sendMail(mailOptions, function (error, response) {

    if (error) {
      console.log(error);
    } else {
      console.log("Message sent: " + response.message);
    }
    return 0;
  });
  return 0;
}

当我使用命令firebase deploy时收到消息 functions:无法更新功能sendVerificationEmail HTTP错误:400,不允许更改函数触发器类型或事件提供程序

我是JS的新手,我不知道这些错误是什么意思

1 个答案:

答案 0 :(得分:0)

删除您的第一个函数sendVerificationEmail,然后重新部署。看来您最初可能将其部署为Firestore触发器以外的其他东西。