3
votes

I want to send a mail once a user is created with a firebase cloud functions, using nodemail and postmark.

I followed this tutorial : Tutorial link from Dave Martin

But keep getting this error:

There was an error while sending the welcome email: { status: 422, message: 'Zero recipients specified', code: 300 }

Here is my code to send a mail from cloud functions:

//Mail 
const nodemailer = require('nodemailer')
const postmarkTransport = require('nodemailer-postmark-transport')


// Google Cloud environment variable used:
// firebase functions:config:set postmark.key="API-KEY-HERE"
const postmarkKey = functions.config().postmark.key
const mailTransport = nodemailer.createTransport(postmarkTransport({
auth: {
    apiKey: postmarkKey
}
}))
exports.OnUserCreation = functions.auth.user().onCreate((user) => 
{
console.log("user created: " + user.data.uid);
console.log("user email: " + user.data.email);
sendEmail(user);
})

function sendEmail(user) 
{
// Send welcome email to new users
const mailOptions = 
{
    from: '"test" <[email protected]>',
    to: user.email,
    subject: 'Welcome!',
    html: 'hello'
}
// Process the sending of this email via nodemailer
return mailTransport.sendMail(mailOptions)
    .then(() => console.log('Welcome confirmation email sent'))
    .catch((error) => console.error('There was an error while sending the welcome email:', error))
}

My postmark.key is already setup in the firebase config... The API tell me the problem is the format I use to send the mail informations.. How could I fix it ?

Update

I also tried to modify the mailOptions as follow and still the same error:

    const mailOptions = {
        from: '[email protected]',
        to: user.email,
        subject: 'Welcome!',
        textBody: 'hello'
    }
1
What if you just use [email protected] fir the From value?Renaud Tarnec
updated, with your suggestion I get the same resultChristophe Chenel
You might want to check if user.email is null, because the documentation suggests that it can be. firebase.google.com/docs/reference/functions/…Doug Stevenson
@DougStevenson I updated my code in the question with your suggestion. I confirm you that the user.email is not null at this moment, it really contain the user.email information and they are formated correctlyChristophe Chenel

1 Answers

9
votes

Decided to restart from scratch by following only postmark documentation (wich is really good by the way).

So here are the very simple steps to send mail from events in firebase cloud functions:

1- download packages:

Run: npm install postmark

2- register to postmark

Register to PostMark - then find your API key.

3- setup firebase environment config:

Run : firebase functions:config:set postmark.key="API-KEY-HERE"

4 index.js code to be added:

//Mail 
const postmark = require('postmark')
const postmarkKey = functions.config().postmark.key;
const mailerClient = new postmark.ServerClient(postmarkKey);

exports.OnUserCreation = functions.auth.user().onCreate((user) => {
console.log("user created: " + user.data.uid);
console.log("user email: " + user.data.email);
return sendEmail(user);
})

// Send welcome email to new users
function sendEmail(user) {
const mailOptions = {
    "From": "[email protected]",
    "To": user.data.email,
    "Subject": "Test",
    "TextBody": "Hello from Postmark!"
}
return mailerClient.sendEmail(mailOptions)
    .then(() => console.log('Welcome confirmation email sent'))
    .catch((error) => console.error('There was an error while sending the welcome email:', error))
}

That's it.

No need to download nodemailer nor use a transporter.