Skip to content

Instantly share code, notes, and snippets.

@bcnzer
Created December 27, 2019 21:35
Show Gist options
  • Save bcnzer/32e6526fb51a1af523d8ee563b3d3d70 to your computer and use it in GitHub Desktop.
Save bcnzer/32e6526fb51a1af523d8ee563b3d3d70 to your computer and use it in GitHub Desktop.
Google Cloud Function for sending an email via SendGrid, triggered by a document being added to Firestore
const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)
const SENDGRID_API_KEY = functions.config().sendgrid.key
const sendGridEmail = require('@sendgrid/mail')
sendGridEmail.setApiKey(SENDGRID_API_KEY)
exports.inviteStudentEmail = functions.firestore
.document('emailtest/{emailtestId}')
.onCreate((event) => {
const emailTestData = event.data()
const msg = {
to: emailTestData.emailAddress,
from: '[email protected]',
subject: 'My subject',
templateId: '<enter your template id here>',
dynamic_template_data: {
clubname: emailTestData.clubname
}
}
return sendGridEmail
.send(msg)
.then(() => console.log('email sent'))
.catch((error) => console.error(error.toString()))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment