Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Functions for Firebase - Send email onWrite

I m trying to send a test email to my email when anything is written in /emails but the email never gets sent and the function logs are empty.

exports.sendTestEmail = functions.database.ref('/emails')
  .onWrite(event => {
    return sendTestEmail('[email protected]');
  })
// [END onWrite]

// Sends a welcome email to the given user.
function sendTestEmail(email) {
  const mailOptions = {
    from: `${APP_NAME} <[email protected]>`,
    to: email
  };
  // The user subscribed to the newsletter.
  mailOptions.subject = `Welcome to ${APP_NAME}!`;
  mailOptions.text = `Hey there! Welcome to ${APP_NAME}. I hope you will enjoy our service.`;
  return mailTransport.sendMail(mailOptions).then(() => {
    console.log('New welcome email sent to:', email);
  });

}

Edit ***

The above code works. I was trying to trigger the function on emails while the correct name was email.

like image 217
Ciprian Avatar asked Jun 08 '17 21:06

Ciprian


Video Answer


1 Answers

Correct way of sending an email using Firebase Cloud Functions!

exports.sendTestEmail = functions.database.ref('/emails')
  .onWrite(event => {
    return sendTestEmail('[email protected]');
  })
// [END onWrite]

// Sends a welcome email to the given user.
function sendTestEmail(email) {
  const mailOptions = {
    from: `${APP_NAME} <[email protected]>`,
    to: email
  };
  // The user subscribed to the newsletter.
  mailOptions.subject = `Welcome to ${APP_NAME}!`;
  mailOptions.text = `Hey there! Welcome to ${APP_NAME}. I hope you will enjoy our service.`;
  return mailTransport.sendMail(mailOptions).then(() => {
    console.log('New welcome email sent to:', email);
  });

}
like image 118
Ciprian Avatar answered Oct 07 '22 02:10

Ciprian