简体   繁体   English

在 nodemailer 中将 object 作为邮件正文发送

[英]Sending object as a mail body in nodemailer

can i send an object key, value pair as a message text in node mailer message object?我可以将 object 密钥、值对作为节点邮件消息 object 中的消息文本发送吗? Its only accepts a plain text but i want to send an object??它只接受纯文本,但我想发送 object?? This is the error its shows => The "chunk" argument must be of type string or an instance of Buffer or Uint8Array.这是它显示的错误=>“块”参数必须是字符串类型或 Buffer 或 Uint8Array 的实例。 Received an instance of Object.收到 Object 的实例。

    const message = {
        from: email,
        to: req.body.toEmail,
        subject: req.body.subject,
        text: req.body.message
    };
    transporter.sendMail(message, function (err, info) {
        if (err) {
            console.log(err)
        } else {
            console.log(info)
            res.status(200).send(info)
        }
    })

You can first convert the object into a string using JSON.stringify您可以首先使用JSON.stringify将 object 转换为字符串

const message = {
    from: email,
    to: req.body.toEmail,
    subject: req.body.subject,
    text: req.body.message
};

let stringMessage = JSON.stringify(message);

Next you can send the object as a string.接下来,您可以将 object 作为字符串发送。

transporter.sendMail( stringMessage , function(err , info){
  // Callback
})

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

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