Reputation: 2527
How to send emails from Firebase without exposing the password?
Following is the use of node mailer for sending firebase emails using cloud functions.
But here we will have to create a transporter with an email and password. Is it possible to do it without a transporter (email and password) . i.e. anonymously. Cause storing password is not safe for this kind of applications.
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: 'test', // email subject
html: `<p style="font-size: 16px;">test it!!</p>
<br />
` // email content in HTML
};
// returning result
return transporter.sendMail(mailOptions, (erro, info) => {
if(erro){
return res.send(erro.toString());
}
return res.send('Sended');
});
});
});
Upvotes: 0
Views: 309
Reputation: 50850
You could set the credentials in function's environment configuration.
To store the email and password, you can use the following command:
firebase functions:config:set nodemailer.email="[email protected]" nodemailer.password="your_password"
After running
functions:config:set
, you must redeploy functions to make the new configuration available.
To use the config in your function, you use the functions.config()
object:
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: functions.config().nodemailer.email,
pass: functions.config().nodemailer.password
}
});
Upvotes: 2