Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How change the sender name of a mail with nodemailer?

I use Nodemailer with a gmail account and I would like to change the sender name. But I don't find how to do. The sender name is actually my email address, but I prefer to have "Consultation". enter image description here This is my code:

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

Could you help me please ? Thanks in advance

like image 894
Malaury Boudon Avatar asked Nov 01 '19 04:11

Malaury Boudon


2 Answers

According to https://nodemailer.com/message/addresses/ you can write:

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

With this, you do not need to worry about the formatting of the name you provide

like image 190
Engineersticity Avatar answered Oct 13 '22 17:10

Engineersticity


Try to send it like below

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>`
        }
like image 26
bhuvnesh pattnaik Avatar answered Oct 13 '22 19:10

bhuvnesh pattnaik