9

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('myemail@gmail.com');
  })
// [END onWrite]

// Sends a welcome email to the given user.
function sendTestEmail(email) {
  const mailOptions = {
    from: `${APP_NAME} <noreply@example.com>`,
    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.

Ciprian
  • 3,066
  • 9
  • 62
  • 98
  • Are you using gmail as your smtp service? If you are, you may need to use an app password instead of your personal password due to Google's security. Here's a way to do that https://support.google.com/accounts/answer/185833?hl=en – Tobi Akerele Jun 09 '17 at 15:34
  • yes I am. the problem is that I send emails using the same format when users create accounts. and that works. – Ciprian Jun 09 '17 at 15:36
  • Try catching errors on the send: `mailTransport.sendMail(mailOptions).then(...).catch(error => { console.error('Error sending:', error); });` – Bob Snyder Jun 12 '17 at 05:22
  • You say "the function logs are empty". You are seeing the entry that says _Function execution started_, right? – Bob Snyder Jun 12 '17 at 05:48
  • Wait ... this is so dumb. I have emails instead of email :). No wonder it wasn't running. – Ciprian Jun 12 '17 at 06:24
  • 1
    Also ensure that the value written to `/emails` is a change from the previous value. I haven't tested all the client SDKs, but with Android, performing a write to a location that does not _change_ the value will not cause the `onWrite()` event to fire. – Bob Snyder Jun 12 '17 at 14:10
  • I will. Thank you! – Ciprian Jun 12 '17 at 14:29

1 Answers1

4

Correct way of sending an email using Firebase Cloud Functions!

exports.sendTestEmail = functions.database.ref('/emails')
  .onWrite(event => {
    return sendTestEmail('myemail@gmail.com');
  })
// [END onWrite]

// Sends a welcome email to the given user.
function sendTestEmail(email) {
  const mailOptions = {
    from: `${APP_NAME} <noreply@example.com>`,
    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);
  });

}
Ciprian
  • 3,066
  • 9
  • 62
  • 98
  • I am new to firebase cloud function, should I import any class to send email according to your solution? What should I write ? – GPH Aug 21 '18 at 10:58