【问题标题】:How to send emails from Firebase without exposing password?如何在不暴露密码的情况下从 Firebase 发送电子邮件?
【发布时间】:2021-07-01 07:34:09
【问题描述】:

如何在不暴露密码的情况下从 Firebase 发送电子邮件?

以下是使用节点邮件程序使用云功能发送 Firebase 电子邮件。

但是在这里我们必须创建一个带有电子邮件和密码的传输器。 是否可以在没有传输器(电子邮件和密码)的情况下做到这一点。即匿名。导致存储密码对于此类应用程序不安全。

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: 'yourgmailaccount@gmail.com',
        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 <yourgmailaccount@gmail.com>', // Something like: Jane Doe <janedoe@gmail.com>
            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');
        });
    });    
});

【问题讨论】:

标签: javascript firebase google-cloud-functions


【解决方案1】:

您可以在函数的environment configuration 中设置凭据。

要存储邮箱和密码,可以使用以下命令:

firebase functions:config:set nodemailer.email="username@domain.tld" nodemailer.password="your_password"

运行functions:config:set 后,您必须重新部署函数以使新配置可用。

要在函数中使用配置,请使用 functions.config() 对象:

let transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: functions.config().nodemailer.email,
        pass: functions.config().nodemailer.password
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 2011-02-08
    • 2012-08-22
    • 2021-03-13
    • 1970-01-01
    • 2015-08-13
    相关资源
    最近更新 更多