22

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

Malaury Boudon
  • 656
  • 1
  • 8
  • 18
  • I tried all of the suggested solutions, but none worked. I'm also following what https://community.nodemailer.com/address-formatting suggests, but with no success. How did you succeed? – marcolav Apr 20 '21 at 09:04

3 Answers3

37

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>`
        }
bhuvnesh pattnaik
  • 1,365
  • 7
  • 14
21

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

Engineersticity
  • 351
  • 2
  • 5
8

by the docs (https://nodemailer.com/message/addresses/) you should write

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

and it works!

ינון רחמים
  • 566
  • 1
  • 5
  • 12