Firebase无法响应云功能

时间:2019-11-01 12:56:13

标签: reactjs firebase firebase-realtime-database

我正在使用React JS,联系表格和firebase初始化的数据添加到实时数据库中。一切正常。

但是,我正在尝试实现在提交新的联系表单后发送给我的电子邮件。当前问题:部署云功能时,当我提交表单(并添加了实时数据库)时,什么都没有发生。在Firebase控制台中甚至没有错误消息。

请您看看我的代码,并提供一些有关如何发送自动电子邮件的建议。

const functions = require('firebase-functions')
const admin = require('firebase-admin');
const nodemailer = require('nodemailer');
admin.initializeApp()
require('dotenv').config()

const email = process.env.REACT_APP_SENDER_EMAIL;
const pass = process.env.REACT_APP_SENDER_PASS;

exports.sendEmailNotification = functions.firestore.document('messages/{id}')
  .onCreate((snap, ctx) => {
    const data = snap.data();
    let authData = nodemailer.createTransport({
      host: 'smtp.gmail.com',
      port: 465,
      secure: true,
      auth: {
        user: email,
        pass: pass
      }
    });
    authData.sendMail({
      from: data.email,
      to: data.to,
      subject: data.name + ' sent a message',
      text: data.text,
    }).then(res => console.log('email sent')).catch(err => console.log(err));
  });

2 个答案:

答案 0 :(得分:1)

您的函数需要返回一个承诺,该承诺将在所有异步工作完成后解决。

return authData.sendMail({
  from: data.email,
  to: data.to,
  subject: data.name + ' sent a message',
  text: data.text,
})

兑现这一承诺可使Cloud Functions知道何时可以安全清理和继续运行。

答案 1 :(得分:0)

我首先也尝试将其创建为Firebase云功能,但是我转向在服务器上构建nodemailer。在Firebase云功能中工作时,我使用了降级到node:package.json中的8的漏洞(已弃用),并且被迫制定Google Firebase付费计划。这两个项目都把我逼到了我不想进入的角落。

由于https://www.youtube.com/watch?v=nF9g1825mwk

,这是node.js中nodemailer的结果
    const express = require('express')
require('dotenv').config()
const bodyParser = require('body-parser')
const exphbs = require('express-handlebars')
const path = require('path')
const nodemailer = require('nodemailer')


const app = express()

const email_from = process.env.EMAIL_FROM;
const sender_pass = process.env.SENDER_PASS;
const email_to = process.env.EMAIL_TO;


// View engine setup
app.engine('handlebars', exphbs())
app.set('view engine', 'handlebars')

//body parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());


//static folder
app.use('/public', express.static(path.join(__dirname, 'public')))

app.get('/', (req, res) => {
    res.render('contact', { layout: false })
})

app.post('/send', (req, res) => {
    const output = `
    <p>You have a new submission</p>
    <h3>Contact Details</h3>
    <ul>
    <li>Name: ${req.body.name}</li>
    <li>Company: ${req.body.company}</li>
    <li>Email: ${req.body.email}</li>
    <li>Phone: ${req.body.phone}</li>
   
    </ul>
    <h3>Message</h3>
    <p> ${req.body.message} </p> `;

    async function main() {
        // Generate test SMTP service account from ethereal.email
        // Only needed if you don't have a real mail account for testing
        // let testAccount = await nodemailer.createTestAccount();

        // create reusable transporter object using the default SMTP transport
        let transporter = nodemailer.createTransport({
            host: "smtp.ethereal.email",
            port: 587,
            secure: false, // true for 465, false for other ports
            auth: {
                user: email_from,
                pass: sender_pass
            },
        });

        // send mail with defined transport object
        let info = await transporter.sendMail({
            from: email_from,
            to: email_to,
            subject: 'New Submission from Dean Productions!',
            text: 'new submission',
            html: output,
        });

        console.log("Message sent: %s", info.messageId);
        // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>

        // Preview only available when sending through an Ethereal account
        console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
        // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...

        res.render('contact', { layout: false, msg: 'Message has been sent!' })
    }

    main().catch(console.error);
})

app.listen(3000, () => console.log('Server started...'))