Skip to content

Instantly share code, notes, and snippets.

@alan89
Last active August 24, 2021 02:00
Show Gist options
  • Save alan89/9544c192de363dc4c2396dadf590f258 to your computer and use it in GitHub Desktop.
Save alan89/9544c192de363dc4c2396dadf590f258 to your computer and use it in GitHub Desktop.
This is a firebase cloud function example that sends a custom action email through Sendgrid.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const sgMail = require('@sendgrid/mail');
// Using @sendgrid/mail
// TODO: Configure the `sendgrid.key` and `sendgrid.template` Google Cloud environment variables.
const API_KEY = functions.config().sendgrid.key;
const TEMPLATE_ID = functions.config().sendgrid.template;
sgMail.setApiKey(API_KEY);
// Sends an email confirmation when a user is created.
exports.sendVerificationEmail = functions
.auth
.user()
.onCreate((user) => {
admin.auth().generateEmailVerificationLink(user.email)
.then(async (link) => {
// Construct email verification template, embed the link and send
// using Sendgrid.
const msg = {
to: user.email,
from: sender.email,
templateId: TEMPLATE_ID,
dynamic_template_data: {
confirmationLink: link,
username: user.displayName,
},
};
return sgMail.send(msg);
})
.catch((error) => {
console.error(error);
return '200';
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment