简体   繁体   English

发送电子邮件给新用户firebase

[英]Sending email to new user firebase

I am trying to send email to new user once he registers. 我试图在新用户注册后向其发送电子邮件。 Firebase github has a neat little code already there. Firebase github已经有一个简洁的小代码。 I am very new to JS and Node in general, so I try to make sense of the code and tried to put it in my index.js and trying to deploy it but I am having issue as below. 一般来说,我对JS和Node还是很陌生,因此我尝试理解代码并将其放入index.js并尝试进行部署,但遇到以下问题。

✔  functions: Finished running predeploy script.
i  functions: ensuring necessary APIs are enabled...
✔  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...

Error: Error occurred while parsing your function triggers.

TypeError: Cannot read property 'email' of undefined
    at Object.<anonymous> (/Users/bharath/cloud-functions/elegal/functions/index.js:11:44)
    at Module._compile (module.js:649:30)
    at Object.Module._extensions..js (module.js:660:10)
    at Module.load (module.js:561:32)
    at tryModuleLoad (module.js:501:12)
    at Function.Module._load (module.js:493:3)
    at Module.require (module.js:593:17)
    at require (internal/module.js:11:18)
    at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:18:11
    at Object.<anonymous> (/usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:38:3)

So, as you can see in the above there seems to be an issue retrieving the email. 因此,正如您在上面看到的那样,检索电子邮件似乎存在问题。 My firebase structure is as below: 我的firebase结构如下:

在此处输入图片说明

Code that is currently in my index.js is 目前在我的index.js中的代码是

'use strict';

const functions = require('firebase-functions');
const nodemailer = require('nodemailer');
// Configure the email transport using the default SMTP transport and a GMail account.
// For Gmail, enable these:
// 1. https://www.google.com/settings/security/lesssecureapps
// 2. https://accounts.google.com/DisplayUnlockCaptcha
// For other types of transports such as Sendgrid see https://nodemailer.com/transports/
// TODO: Configure the `gmail.email` and `gmail.password` Google Cloud environment variables.
const gmailEmail = functions.config().gmail.email;
const gmailPassword = functions.config().gmail.password;
const mailTransport = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'asiarubber@gmail.com',
    pass: 'asiarubber2123',
  },
});

// Your company name to include in the emails
// TODO: Change this to your app or company name to customize the email sent.
const APP_NAME = 'Cloud Storage for Firebase quickstart';

// [START sendWelcomeEmail]
/**
 * Sends a welcome email to new user.
 */
// [START onCreateTrigger]
exports.sendWelcomeEmail = functions.auth.user().onCreate((event) => {
  // [END onCreateTrigger]
  // [START eventAttributes]
  const user = event.data; // The Firebase user.

  const email = user.email; // The email of the user.
  const displayName = user.displayName; // The display name of the user.
  // [END eventAttributes]

  return sendWelcomeEmail(email, displayName);
});
// [END sendWelcomeEmail]

// [START sendByeEmail]
/**
 * Send an account deleted email confirmation to users who delete their accounts.
 */
// [START onDeleteTrigger]
exports.sendByeEmail = functions.auth.user().onDelete((event) => {
  // [END onDeleteTrigger]
  const user = event.data;

  const email = user.email;
  const displayName = user.displayName;

  return sendGoodbyEmail(email, displayName);
});
// [END sendByeEmail]

// Sends a welcome email to the given user.
function sendWelcomeEmail(email, displayName) {
  const mailOptions = {
    from: `${APP_NAME} <noreply@firebase.com>`,
    to: email,
  };

  // The user subscribed to the newsletter.
  mailOptions.subject = `Welcome to ${APP_NAME}!`;
  mailOptions.text = `Hey ${displayName || ''}! Welcome to ${APP_NAME}. I hope you will enjoy our service.`;
  return mailTransport.sendMail(mailOptions).then(() => {
    return console.log('New welcome email sent to:', email);
  });
}

// Sends a goodbye email to the given user.
function sendGoodbyEmail(email, displayName) {
  const mailOptions = {
    from: `${APP_NAME} <noreply@firebase.com>`,
    to: email,
  };

  // The user unsubscribed to the newsletter.
  mailOptions.subject = `Bye!`;
  mailOptions.text = `Hey ${displayName || ''}!, We confirm that we have deleted your ${APP_NAME} account.`;
  return mailTransport.sendMail(mailOptions).then(() => {
    return console.log('Account deletion confirmation email sent to:', email);
  });
}

Link to the github 链接到github

Thankyou for your help. 谢谢您的帮助。 Once again, I am pretty new to JS and Node in general and cloud functions even more so. 再说一次,对于JS和Node来说,我还是一个新手,而云功能则更是如此。

Firstly check if firebase function is being called out. 首先检查firebase函数是否被调出。

Secondly if it's working out fine, you should check for less secure option in your gmail's security option in profile setting & also check for block capitcha option to enable it's access. 其次,如果工作正常,则应在配置文件设置的gmail的安全性选项中检查安全性较低的选项,并还要检查block capitcha选项以启用它的访问权限。

Thirdly check the logs in firebase cloud function, that provides a link to login first from that link, if you are already login in browser, log out and then login using the link provided in firebase cloud function's logs. 第三,检查Firebase Cloud功能中的日志,该日志提供了一个从该链接登录的链接,如果您已经在浏览器中登录,请先注销,然后再使用Firebase Cloud功能日志中提供的链接登录。

Fourth Setting in node js or cmd the credentials for the email : 在节点js或cmd中的第四项设置电子邮件的凭据:

firebase functions:config:set gmail.email="*****@gmail.com" gmail.password="******" firebase功能:config:set gmail.email =“ ***** @ gmail.com” gmail.password =“ ******”

Have a great day. 祝你有美好的一天。 Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM