使用带有Vue Js的Firebase云功能发送电子邮件时出错

yvgpqqbh  于 2023-03-24  发布在  Vue.js
关注(0)|答案(1)|浏览(122)

我试图发送电子邮件使用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)

我该怎么补救呢?

iqxoj9l9

iqxoj9l91#

执行以下操作无法正常工作:

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

你混合了Callable Cloud FunctionsHTTPS ones(它们以Node.js表达式reqres作为参数)。reqres将没有任何值,因此dest将为null。
我建议你研究一下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

相关问题