通过带有 Express 和 Node.js 的 Nodemailer 包括多个表单字段

Including multiple form fields via Nodemailer with Express and Node.js

每次用户提交我的联系表时,我都试图收到一封电子邮件。 该表单具有以下字段:

- 电子邮件

- 主题

- 姓名

- 曲目

- 数字

- 消息

然而,当发送电子邮件时,我只能获得我在邮件选项下的 "text" 字段中输入的任何字段。如果我尝试将其转换为数组或对象,它将不起作用,因为它说块需要是字符串。

如何发送比我在文本中输入的内容更多的表单字段?这是我的代码:

Nodemailer POST 路由:

app.post ("/contact" , urlencodedParser,  function(req, res){
    const transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'theemail',
    pass: 'emailpassword' // naturally, replace both with your real credentials or an application-specific password
  }
});

var subject = req.body.subject;
var email = req.body.email;

var data = [ 
    req.body.name,
    req.body.track,
    req.body.number,
    req.body.message,
]


const mailOptions = {
  from: req.body.email,
  subject: req.body.subject,
  to: "theemail",
  text: req.body.message,


};

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

    res.render("../EN/views/contact-success");
});

表格HTML

<div class="fade-in serviceform text-center container form-group">
    <form method ="POST" action = "/contact">
    <p class=" text-center">Touring | Recording | Online sessions | Any other question</p>
    <input type="email" name="email" class="contactform" placeholder=" E-mail" required>
    <input type="text" name="subject" class="contactform" placeholder=" Subject" required>
    <input type="text" name="name" class="contactform" placeholder=" First and last name" required>
    <input type="text" name="track" class="contactform" placeholder=" Track Name" required>
    <input type="number" name="number" class="contactform" placeholder=" +351919999999" required>
    <textarea class="contactformtext" name="message" placeholder=" Write your message here." rows="3" required></textarea>
    <div class="text-center">
        <button type="submit" class="btn btn-outline-danger">Contact</button>       
    </div>
</form>
<div class ="text-center">
    <a class="anchor" href="https://www.facebook.com/"><i class="fab fa-facebook-square"></i></a><a class="anchor" href="https://www.instagram.com//"><i class="fab fa-instagram"></i></a>
<p>Facebook  | | Instagram  </p>
</div>
```

只需使用反引号,它就会将自己视为字符串