How to send customize e-mail with Firebase cloud functions











up vote
1
down vote

favorite












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'
}









share|improve this question




















  • 1




    What if you just use test@test.com fir the From value?
    – Renaud Tarnec
    Nov 17 at 19:57










  • updated, with your suggestion I get the same result
    – Christophe Gudlake
    Nov 17 at 20:07










  • 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
    Nov 17 at 20:17










  • @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 correctly
    – Christophe Gudlake
    Nov 17 at 20:20















up vote
1
down vote

favorite












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'
}









share|improve this question




















  • 1




    What if you just use test@test.com fir the From value?
    – Renaud Tarnec
    Nov 17 at 19:57










  • updated, with your suggestion I get the same result
    – Christophe Gudlake
    Nov 17 at 20:07










  • 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
    Nov 17 at 20:17










  • @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 correctly
    – Christophe Gudlake
    Nov 17 at 20:20













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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'
}









share|improve this question















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'
}






node.js firebase google-cloud-functions nodemailer postmark






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 at 17:52

























asked Nov 17 at 19:46









Christophe Gudlake

575426




575426








  • 1




    What if you just use test@test.com fir the From value?
    – Renaud Tarnec
    Nov 17 at 19:57










  • updated, with your suggestion I get the same result
    – Christophe Gudlake
    Nov 17 at 20:07










  • 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
    Nov 17 at 20:17










  • @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 correctly
    – Christophe Gudlake
    Nov 17 at 20:20














  • 1




    What if you just use test@test.com fir the From value?
    – Renaud Tarnec
    Nov 17 at 19:57










  • updated, with your suggestion I get the same result
    – Christophe Gudlake
    Nov 17 at 20:07










  • 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
    Nov 17 at 20:17










  • @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 correctly
    – Christophe Gudlake
    Nov 17 at 20:20








1




1




What if you just use test@test.com fir the From value?
– Renaud Tarnec
Nov 17 at 19:57




What if you just use test@test.com fir the From value?
– Renaud Tarnec
Nov 17 at 19:57












updated, with your suggestion I get the same result
– Christophe Gudlake
Nov 17 at 20:07




updated, with your suggestion I get the same result
– Christophe Gudlake
Nov 17 at 20:07












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
Nov 17 at 20:17




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
Nov 17 at 20:17












@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 correctly
– Christophe Gudlake
Nov 17 at 20:20




@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 correctly
– Christophe Gudlake
Nov 17 at 20:20












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354930%2fhow-to-send-customize-e-mail-with-firebase-cloud-functions%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    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.






    share|improve this answer

























      up vote
      2
      down vote



      accepted










      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.






      share|improve this answer























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 17 at 20:50









        Christophe Gudlake

        575426




        575426






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354930%2fhow-to-send-customize-e-mail-with-firebase-cloud-functions%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin