Nodemailer with an alias Gmail account (custom domain)

I will divide the process into 3 steps:

  1. Activate 2-Step Verification
  2. Add alias to your Gmail
  3. Configure Nodemailer

Activate 2-Step Verfiication

  1. Go to your Google account
  2. Go to "Security" and activate 2-Step Verification
  3. Go to "Security" and add app passwords:
    • Select app: 'Other (Custom name)'
    • Write the name of the domain, e.g. example.com
  4. Copy the 16-character password generated

Add alias to your Gmail

  1. Go to your Gmail account
  2. Click on settings and go to "Accounts and Import"
  3. Go to "Send mail as" and click on "Add another email address"
  4. A new yellowish window will open:
    • Name: Your name
    • Email address: hi@example.com
    • "Treat as an alias" should be activated
  5. Next step:
    • SMTP Server: smtp.gmail.com
    • Port: 465
    • User Name: youremail@gmail.com
    • Password: paste the 16-character password
    • "Secured connection using SSL" should be activated
  6. Add account

Nodemailer configuration

javascript
const nodemailer = require('nodemailer');

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Mail configuration
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const mailTransport = nodemailer.createTransport({
    
    service: 'gmail',
    auth: {
        user: 'youremail@gmail.com',
        pass: 'examplepasvdfxou'
    }
    
});

const mail = {

    from: 'alias@yourdomain.com',
    to: 'whatever@whatever.com',
    subject: 'Whatever',
    text: 'Erik Martín is an awesome engineer.'

};

mailTransport.sendMail(mail);    

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.