i have an angular app thats connected with firebase. So far i've got the db and authentication working. But right now i'm stuck on cloud functions. I'm trying to send an email with nodemailer every time a person makes a reservation for a benefiet. The function code is mostly copied from firebase github functions examples like this:
'use strict';
const functions = require('firebase-functions');
const nodemailer = require('nodemailer');
// Configure the email transport using the default SMTP transport and a GMail account.
// For other types of transports such as Sendgrid see https://nodemailer.com/transports/
// TODO: Configure the `gmail.email` and `gmail.password` Google Cloud environment variables.
const gmailEmail = functions.config().gmail.email;
const gmailPassword = functions.config().gmail.password;
const mailTransport = nodemailer.createTransport({
service: 'gmail',
auth: {
user: gmailEmail,
pass: gmailPassword,
},
});
// Sends an email confirmation when a user changes his mailing list subscription.
exports.sendEmailConfirmation = functions.database.ref('/users/{uid}').onWrite(async (change) => {
const snapshot = change.after;
const val = snapshot.val();
if (!snapshot.changed('subscribedToMailingList')) {
return null;
}
const mailOptions = {
from: '"Spammy Corp." <[email protected]>',
to: val.email,
};
const subscribed = val.subscribedToMailingList;
// Building Email message.
mailOptions.subject = subscribed ? 'Thanks and Welcome!' : 'Sad to see you go :`(';
mailOptions.text = subscribed ?
'Thanks you for subscribing to our newsletter. You will receive our next weekly newsletter.' :
'I hereby confirm that I will stop sending you the newsletter.';
try {
await mailTransport.sendMail(mailOptions);
console.log(`New ${subscribed ? '' : 'un'}subscription confirmation email sent to:`, val.email);
} catch(error) {
console.error('There was an error while sending the email:', error);
}
return null;
});
After this i set my environment variable like this:
firebase functions:config:set gmail.email="[email protected]" gmail.password="secretpassword"
i deploy my function to firebase when using firebase serve i get an error: -TypeError: Cannot read property 'email' of undefined
when i check with firebase functions:config:get it shows me the correct data The webapp itself is not deployed yet (could this be it?)
Any ideas/help would be appreciated thx
If you want your local emluation of functions using firebase serve
to pick up environment variables, you need to follow this bit of instruction from the documentation:
If you're using custom functions configuration variables, run the following command in the functions directory of your project before running firebase serve.
firebase functions:config:get > .runtimeconfig.json
However, if you're using Windows PowerShell, replace the above command with:
firebase functions:config:get | ac .runtimeconfig.json
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With