Skip to content

Instantly share code, notes, and snippets.

@ancs21
Created November 18, 2018 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ancs21/e0dd32c3640e0f6b56bce6836dac5833 to your computer and use it in GitHub Desktop.
Save ancs21/e0dd32c3640e0f6b56bce6836dac5833 to your computer and use it in GitHub Desktop.
Send mail firebase function api
import * as functions from 'firebase-functions'
import * as nodemailer from 'nodemailer'
import * as cors from 'cors'
const corsHandler = cors({ origin: true })
export const form = functions.https.onRequest((req, res) => {
const corsFn = cors()
corsFn(req, res, function() {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
const { name, email, budget, message } = req.query
const transporter = nodemailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
auth: {
user: 'YOUR_EMAIL',
pass: 'YOUR_MAIL_PASSWORD'
}
})
const mailOptions = {
from: 'YOUR_SENDER_EMAIL',
to: 'YOUR_RECEIVE_MAIL',
subject: '🏹 Submit form from.... ✅',
html: `
Name: <b>${name}</b><br/>
Email: <b>${email}</b><br/>
Budget: <b>${budget}</b><br/>
Message: <b>${message}</b><br/>
`
}
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error)
res.status(402).json({
error: 'Mail send error. Please try again'
})
}
console.log(info)
res.status(200).json({
message: 'Mail send success'
})
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment