node.jsfirebasegoogle-cloud-functionsnodemailerpostmark

How to send customize e-mail with Firebase cloud functions


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" <test@test.com>',
    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: 'test@test.com',
        to: user.email,
        subject: 'Welcome!',
        textBody: 'hello'
    }

Solution

  • 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": "XYZ@YOURDOMAIN.com",
        "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.