如何使用 nodemailer 更改邮件的发件人姓名?

How change the sender name of a mail with nodemailer?

我将 Nodemailer 与 gmail 帐户一起使用,我想更改发件人姓名。 但我不知道该怎么做。 发件人姓名实际上是我的电子邮件地址,但我更喜欢 "Consultation"。 这是我的代码:

var smtpTransport = mailer.createTransport("SMTP", {
    service: "Gmail",
    auth: {
        user: "m***@gmail.com",
        pass: "****"
    }},
    {from: 'Consultation'}
);

module.exports = {
    sendNewUserEmail: function (user) {
        var mail = {
            from: "Consultation",
            to: "i***@gmail.com",
            subject: `Consultation: New client ${user.firstname} ${user.lastname}`,
            html: `<h1>New client for kitomba</h1>
            <ul>
                <li>Firstname: ${user.firstname}</li>
                <li>Lastname: ${user.lastname}</li>
                <li>Phone: ${user.phone}</li>
                <li>Mail: ${user.mail}</li>
                <li>Address: ${user.address}</li>
                <li>Suburb: ${user.suburb}</li>
                <li>State: ${user.state}</li>
                <li>Postal code: ${user.postalcode}</li>
            </ul>`
        }

        smtpTransport.sendMail(mail, function (error, response) {
            if (error) {
                console.log("Error");
                console.log(error);
            } else {
                console.log("Mail send!")
            }
            smtpTransport.close();
        });
    }}

你能帮帮我吗? 提前致谢

尝试像下面这样发送

let from = `Consultation <i***@gmail.com>`

var mail = {
            from: from,
            to: "i***@gmail.com",
            subject: `Consultation: New client ${user.firstname} ${user.lastname}`,
            html: `<h1>New client for kitomba</h1>
            <ul>
                <li>Firstname: ${user.firstname}</li>
                <li>Lastname: ${user.lastname}</li>
                <li>Phone: ${user.phone}</li>
                <li>Mail: ${user.mail}</li>
                <li>Address: ${user.address}</li>
                <li>Suburb: ${user.suburb}</li>
                <li>State: ${user.state}</li>
                <li>Postal code: ${user.postalcode}</li>
            </ul>`
        }

根据文档 (https://nodemailer.com/message/addresses/) 你应该写

from: "Consultation <m***@gmail.com>"

而且有效!

根据https://nodemailer.com/message/addresses/你可以这样写:

  from: {
    name: 'Consultation',
    address: 'm***@gmail.com'
}

有了这个,您就不必担心您提供的名称的格式了