Skip to content

Instantly share code, notes, and snippets.

@jsorkin24
Last active May 13, 2020 19:40
Show Gist options
  • Select an option

  • Save jsorkin24/cc126702c0859856b77520f4733f07d4 to your computer and use it in GitHub Desktop.

Select an option

Save jsorkin24/cc126702c0859856b77520f4733f07d4 to your computer and use it in GitHub Desktop.
functions/index.js folder for Contact Form
const functions = require('firebase-functions')
const nodemailer = require('nodemailer')
const cors = require('cors')({
origin: true
})
const gmailEmail = functions.config().gmail.email
const gmailPassword = functions.config().gmail.password
const mailTransport = nodemailer.createTransport({
service: 'gmail',
auth: {
user: gmailEmail,
pass: gmailPassword,
},
})
exports.submit = functions.https.onRequest((req, res) => {
res.set('Access-Control-Allow-Origin', '*')
res.set('Access-Control-Allow-Methods', 'GET, PUT, POST, OPTIONS')
res.set('Access-Control-Allow-Headers', '*')
if (req.method === 'OPTIONS') {
res.end()
} else {
cors(req, res, () => {
if (req.method !== 'POST') {
return
}
const mailOptions = {
from: req.body.email,
replyTo: req.body.email,
to: gmailEmail,
subject: `${req.body.name} just messaged me from my website`,
text: req.body.message,
html: `<p>${req.body.message}</p>`,
}
return mailTransport.sendMail(mailOptions).then(() => {
console.log('New email sent to:', gmailEmail)
res.status(200).send({
isEmailSend: true
})
return
})
})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment