Created
December 27, 2019 21:35
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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