I will divide the process into 3 steps:
- Activate 2-Step Verification
- Add alias to your Gmail
- Configure Nodemailer
Activate 2-Step Verfiication
- Go to your Google account
- Go to "Security" and activate 2-Step Verification
- Go to "Security" and add app passwords:
- Select app: 'Other (Custom name)'
- Write the name of the domain, e.g. example.com
- Copy the 16-character password generated
Add alias to your Gmail
- Go to your Gmail account
- Click on settings and go to "Accounts and Import"
- Go to "Send mail as" and click on "Add another email address"
- A new yellowish window will open:
- Name: Your name
- Email address: hi@example.com
- "Treat as an alias" should be activated
- 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
- Add account
Nodemailer configuration
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);