Last active
November 24, 2023 04:22
-
-
Save geeksilva97/0a889a9a29f530fa195397bf20061826 to your computer and use it in GitHub Desktop.
Sending email with Cloud Functions for Firebase
This file contains 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 admin = require('firebase-admin'); | |
const nodemailer = require('nodemailer'); | |
const cors = require('cors')({origin: true}); | |
admin.initializeApp(); | |
/** | |
* Here we're using Gmail to send | |
*/ | |
let transporter = nodemailer.createTransport({ | |
service: 'gmail', | |
auth: { | |
user: '[email protected]', | |
pass: 'yourgmailaccpassword' | |
} | |
}); | |
exports.sendMail = functions.https.onRequest((req, res) => { | |
cors(req, res, () => { | |
// getting dest email by query string | |
const dest = req.query.dest; | |
const mailOptions = { | |
from: 'Your Account Name <[email protected]>', // Something like: Jane Doe <[email protected]> | |
to: dest, | |
subject: 'I\'M A PICKLE!!!', // email subject | |
html: `<p style="font-size: 16px;">Pickle Riiiiiiiiiiiiiiiick!!</p> | |
<br /> | |
<img src="https://images.prod.meredith.com/product/fc8754735c8a9b4aebb786278e7265a5/1538025388228/l/rick-and-morty-pickle-rick-sticker" /> | |
` // email content in HTML | |
}; | |
// returning result | |
return transporter.sendMail(mailOptions, (erro, info) => { | |
if(erro){ | |
return res.send(erro.toString()); | |
} | |
return res.send('Sended'); | |
}); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment