简体   繁体   English

通过 netlify 函数发送 email

[英]sending email via netlify functions

Im trying to write a lambda function to send a mail to a gmail adress, so i have a form in vuejs that post to this function: Im trying to write a lambda function to send a mail to a gmail adress, so i have a form in vuejs that post to this function:

const nodemailer = require('nodemailer');
const qs = require("querystring");

exports.handler = function (event, context, callback) {

  const {
    name,
    email,
    phone,
    message
  } = qs.parse(event.body);

  const sendMail = (name, email, phone, message) => {
      const transporter = nodemailer.createTransport({
      service: "gmail",
      auth: {
        user: "xxxxxx@gmail.com",
        pass: "xxxxxxx"
      }
    });
    const mailOptions = {
      from: email, // sender address
      to: "xxxxxxx@gmail.com", // list of receivers
      subject: `Message from ${name} to FoodAllergyFriendly website`, // Subject line
      html: `${name} avec l'adresse ${email} et le numéro de téléphone ${phone} a écrit ceci: <br> <p>${message}</p>` // plain text body
};
    transporter.sendMail(mailOptions, function (err, info) {
      if (err) console.log(err);
      else console.log(info);
    });
  };
   sendMail();
   callback(null, {
     statusCode: 200,
      body: "Merci !"
   });
}

the message is well send but i have undefined for name, email, phone and message in my email.消息很好发送,但我的 email 中的名称、email、电话和消息未定义。 It seems i don't parse the body of my form in the right way.看来我没有以正确的方式解析表单的主体。 Anyone has a clue?有人有线索吗?

Your form POST from Vue.js so use JSON.parse(body) not qs which is used to parse query strings in URL您来自 Vue.js 的表单 POST 所以使用 JSON.parse(body) 而不是 qs 用于解析 URL 中的查询字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM