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

时间:2019-11-01 04:07:32

标签: reactjs nodemailer

我将Gmail帐户与Nodemailer一起使用,我想更改发件人名称。 但是我不知道该怎么办。 发件人姓名实际上是我的电子邮件地址,但是我更喜欢“咨询”。 enter image description here 这是我的代码:

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();
        });
    }}

能帮我吗? 预先感谢

3 个答案:

答案 0 :(得分:2)

您应该写的文档(https://nodemailer.com/message/addresses/

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

它有效!

答案 1 :(得分:1)

尝试按以下方式发送

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>`
        }

答案 2 :(得分:0)

根据https://nodemailer.com/message/addresses/,您可以编写:

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

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