Skip to content

Instantly share code, notes, and snippets.

@felixivance
Created March 7, 2018 12:26
Show Gist options
  • Save felixivance/195bf9a553e10486e0ff18ae3ba78e87 to your computer and use it in GitHub Desktop.
Save felixivance/195bf9a553e10486e0ff18ae3ba78e87 to your computer and use it in GitHub Desktop.
Firebase Cloud Functions HTTP Trigger to send confirmation email utilizing SendGrid
'use strict';
const functions = require('firebase-functions');
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey("SEND_GRID_API_KEY"); /** replace with your own SendGrid API key */
const cors = require('cors')({
origin: ['ANGULAR_DEVELOPMENT_ENVIRONMENT_URL'], /** replace with the url of your development environment that serves your angular app. */
methods: ['POST', 'OPTIONS'],
allowedHeaders: ['Content-Type'],
preflightContinue: false,
optionsSuccessStatus: 204
});
exports.sendEmailConfirmation = functions.https.onRequest((req, res) => {
cors(req, res, () => {
return Promise.resolve()
.then(() => {
if (req.method !== 'POST') {
const error = new Error('Only POST requests are accepted');
error.code = 405;
throw error;
}
const message = {
to: req.body.to,
from: req.body.from,
subject: req.body.subject,
text: req.body.text,
html: req.body.html
};
return sgMail.send(message);
})
.then((response) => {
if (response.body) {
res.send(response.body);
} else {
res.end();
}
})
.catch((err) => {
console.error(err);
return Promise.reject(err);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment