10

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." <noreply@firebase.com>',
    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="myusername@gmail.com" 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

benomatis
  • 5,536
  • 7
  • 36
  • 59
Bob Reyniers
  • 103
  • 1
  • 4

2 Answers2

18

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
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
0

Why not using .env file from the functions directory? It catches the environmental variables automatically.

Daniel Danielecki
  • 8,508
  • 6
  • 68
  • 94
  • Have you testes `.env` to work universally, even after deployed on the site as well, or did you mean to only use this for local testing? Because if latter one, then Doug's answer is better in a way that it will use the actual environment configuration you can use in functions, not having to use 2 different methods. – benomatis Jan 17 '21 at 10:44
  • 1
    In general, also for production. Based on ...Doug's answer (https://stackoverflow.com/a/65080783/11127383). – Daniel Danielecki Jan 20 '21 at 08:28
  • 1
    thanks, that clarifies it, though the "official" way is to use firebase's function config, documented, supported, so probably more practical to use that over `.env`, I'd say... – benomatis Jan 20 '21 at 09:22