【问题标题】:Error while sending email using Firebase cloud functions with Vue Js使用带有 Vue Js 的 Firebase 云功能发送电子邮件时出错
【发布时间】:2020-07-13 12:51:20
【问题描述】:

我尝试使用 Firebase 云功能从表单提交的数据中发送电子邮件,但它根本不发送。

我的功能码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const nodemailer = require('nodemailer');
const cors = require('cors')({
  origin: '*'
})

admin.initializeApp();

/**
 * Here we're using Gmail to send 
 */
let transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'youremail@gmail.com',
    pass: 'password'
  }
});

exports.sendEmail = functions.https.onCall((contact, context) => {
  cors(req, res, () => {
    const dest = req.body.dest

    console.log(dest)

    const mailOptions = {
      from: `Contact AET  ${contact.email}`,
      to: dest,
      subject: "AET Website Message ????????????????", // Subject line
      html: `
        <div 
            style="padding: 10px; font-family:Gadaj; border: 2px solid #eee; border-radius: 10px ">
          <p style="font-size: 15px">You have a new message request</p>
            <h2>Contact Details</h2>
            <ul style="list-style-type: none">
               <li>Name: ${contact.name}</li>
               <li>Email: ${contact.email}</li>
            </ul>
            <h2>Message</h2>
            <p style="font-size: 16px;">${contact.message}</p>
            <img src="https://images.prod.meredith.com/product/fc8754735c8a9b4aebb786278e7265a5/1538025388228/l/rick-and-morty-pickle-rick-sticker" />
        </div>
   `
    };

    return transporter.sendMail(mailOptions, (err, info) => {
      if (err) {
        return console.log(err)
      }
      return console.log(`Message sent to ${info.messageId}`)
    })
  })

})

然后我像这样将它连接到我的前端

sendMessage() {
      if (this.$refs.formEmail.validate()) {
        this.loading = true;
        const createMessage = {
          name: this.name,
          email: this.email,
          message: this.message
        };
        const sendEmail = functions.httpsCallable("sendEmail");
        sendEmail(createMessage)
          .then(res => {
            // eslint-disable-next-line no-console
            console.log(res.data);
          })
          .catch(err => {
            // eslint-disable-next-line no-console
            console.log(err);
          });
      }
    }

我把它作为错误返回:

 Error: Error: INTERNAL
    at new HttpsErrorImpl (index.cjs.js?001a:58)
    at _errorForResponse (index.cjs.js?001a:153)
    at Service.eval (index.cjs.js?001a:538)
    at step (tslib.es6.js?9ab4:99)
    at Object.eval [as next] (tslib.es6.js?9ab4:80)
    at fulfilled (tslib.es6.js?9ab4:70)

我该如何解决这个问题?

【问题讨论】:

  • 仅供参考:如果您对问题有更新,请不要添加评论。您可以直接使用底部的编辑链接编辑问题。

标签: firebase vue.js google-cloud-functions


【解决方案1】:

执行以下操作无效:

exports.sendEmail = functions.https.onCall((contact, context) => {
  cors(req, res, () => {...});
});

您正在混合使用 Callable Cloud FunctionsHTTPS ones(以 Node.js 表示 reqres 作为参数)。 reqres 没有任何值,因此 dest 将为空。

我建议您学习 Callable Cloud Functions 文档。

以下应该可以解决问题(未测试):

exports.sendEmail = functions.https.onCall(async (data, context) => {

    try {

        const dest = data.email

        console.log(dest)

        const mailOptions = {
            from: `Contact AET  ${contact.email}`,
            to: dest,
            subject: "AET Website Message ????", // Subject line
            html: `
        <div 
            style="padding: 10px; font-family:Gadaj; border: 2px solid #eee; border-radius: 10px ">
          <p style="font-size: 15px">You have a new message request</p>
            <h2>Contact Details</h2>
            <ul style="list-style-type: none">
               <li>Name: ${contact.name}</li>
               <li>Email: ${contact.email}</li>
            </ul>
            <h2>Message</h2>
            <p style="font-size: 16px;">${contact.message}</p>
            <img src="https://images.prod.meredith.com/product/fc8754735c8a9b4aebb786278e7265a5/1538025388228/l/rick-and-morty-pickle-rick-sticker" />
        </div>
   `
        };

        await transporter.sendMail(mailOptions);

        return null;

    } catch (error) {

       // See the doc: https://firebase.google.com/docs/functions/callable#handle_errors

    }

})

请注意,我们使用const dest = data.email,因为您用来调用CF 的createMessage 对象中没有dest 元素。


你也可以研究这个样本:https://github.com/firebase/functions-samples/blob/master/quickstarts/email-users/functions/index.js

【讨论】:

  • 我已经尝试过了,先生,非常感谢,我在控制台中收到了这个:Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8
  • 如果你用谷歌搜索 google.com/…,你会发现你需要允许安全性较低的应用从你的谷歌账户发送电子邮件 (stackoverflow.com/a/59194512/3371862)
  • 电子邮件已发送,先生,一切正常,但我从数据中得到了空值{data: null} data: null 与邮件相同传递的数据返回null
  • 你从哪里得到data = null?云函数的data 参数? functions.https.onCall(async (data, context)
  • 非常感谢...而不是我这样做,const createMessage = { name: this.formData.name, email: this.formData.email, message: this.formData.message }; 我引用的是一个空的 V 模型。 const createMessage = { name: this.name, email: this.email, message: this.message };
猜你喜欢
  • 2017-12-03
  • 2020-11-22
  • 2018-11-15
  • 2018-12-19
  • 2021-06-18
  • 2021-08-14
  • 2018-10-16
  • 2019-04-20
  • 1970-01-01
相关资源
最近更新 更多