Created
November 18, 2018 12:29
-
-
Save ancs21/e0dd32c3640e0f6b56bce6836dac5833 to your computer and use it in GitHub Desktop.
Send mail firebase function api
This file contains hidden or 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
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