Created
December 21, 2018 15:59
-
-
Save AlekseyA/5a4ba9fab152e365bfabb6d408dde676 to your computer and use it in GitHub Desktop.
Firebase function what sends email
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
| const functions = require('firebase-functions'); | |
| const nodemailer = require('nodemailer'); | |
| // // Docs | |
| // // https://firebase.google.com/docs/functions/write-firebase-functions | |
| // | |
| exports.sendSupportEmail = functions.https.onRequest(async (request, response) => { | |
| const transporter = nodemailer.createTransport({ | |
| host: 'send.one.com', | |
| auth: { | |
| user: `email`, | |
| pass: `pass`, | |
| } | |
| }); | |
| const send = async (data) => { | |
| const mailBody = { | |
| from: `from@email.com`, | |
| to: 'dest@mail.com', | |
| subject: 'Support request', | |
| text: `User name: ${data.name} | |
| User email: ${data.email} | |
| Text: ${data.text}` | |
| }; | |
| transporter.sendMail(mailBody, (error, info) => { | |
| if (error) { | |
| return res.status(500).json({ message: error.message }); | |
| } else { | |
| return res.send('Email sent: ' + info.response); | |
| } | |
| }); | |
| }; | |
| try { | |
| const data ={ | |
| email: request.body.email || '', | |
| name: request.body.name || '', | |
| text: request.body.text || '', | |
| } | |
| await send(data) | |
| response.send("sent!"); | |
| } catch (error) { | |
| response.send(`Ooops... ${error.message}`); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment