4

I am sending emails from NodeJS using Nodemailer and sestransport on AWS, and am wondering how I can change the 'from name'?

fFr example, for from I put

    const mailOptions =
    {
      from:     '<noreply@example.com>',
      to:       'someone@example.com',
      subject:  'Hi',
    }

When I get the email, it appears to come from 'noreply'. I would like to be able to change to name to anything, for example 'tom hanks', but still have the reply address noreply@example.com. Is that possible?

NeNaD
  • 18,172
  • 8
  • 47
  • 89
user1709076
  • 2,538
  • 9
  • 38
  • 59

4 Answers4

6

Simply specify it before the email address:

const mailOptions =
{
  from:     '"Tom Hanks" <noreply@example.com>',
  to:       'someone@example.com',
  subject:  'Hi',
}
Azami
  • 2,122
  • 1
  • 12
  • 22
2

my bad, i just found out how. just do

const mailOptions =
{
  from:     'Tom Hanks <noreply@example.com>',
  to:       'someone@example.com',
  subject:  'Hi',
}

and then the sender name appears as Tom Hanks

user1709076
  • 2,538
  • 9
  • 38
  • 59
1

You can specify Address object with name and address properties. name property will be friendly-name for the sender, and address field will be the actual sender email.

const mailOptions = {
  from: { name: 'Naruto Uzumaki', address: 'noreply@example.com' },
  to: 'someone@example.com',
  subject: 'Hi',
}
NeNaD
  • 18,172
  • 8
  • 47
  • 89
0

If you don't want to hard code your name you can use template string

const mailOptions = 
{
  from:   `${myNameVariable} <noreplay@example.com>`,
  to:      mySenderVariable,
  subject: mySubjectVariable
}
GisliBG
  • 21
  • 1
  • 2