2026-07-15

node.js email doesn't get sent with gmail smtp

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

node.js email doesn't get sent with gmail smtp

Why Your Node.js Email Fails with Gmail SMTP: A Deep Dive into Authentication Errors

As a developer, sending emails reliably is a fundamental task. When you integrate services like Nodemailer with external SMTP providers, hitting an authentication error can be incredibly frustrating. You have the correct email and password, yet the service throws an EAUTH error, blocking your message delivery.

This post will diagnose why this specific issue occurs when using Gmail's SMTP server with Node.js and provide the practical steps necessary to resolve it, along with best practices for email delivery in modern applications.

Understanding the EAUTH Error

The error you are encountering—specifically { [Error: Invalid login], code: 'EAUTH', response: '535-5.7.8 Username and Password not accepted' }—is a clear signal that the SMTP server (in this case, Google's servers) rejected the credentials provided. This is rarely an issue with the email address or password itself; it almost always points to an authentication mechanism blockage imposed by Google for security reasons.

When using standard Gmail accounts for external applications via SMTP, especially when Two-Factor Authentication (2FA) is enabled (which it should be), simply using your regular account password often fails because Google requires a specific type of access token instead of the login password for third-party apps.

The Root Cause: Google's Security Protocols

Google has tightened security protocols significantly regarding third-party application access to its mail servers. When you try to log in via an SMTP service, Google expects credentials that are specifically authorized for this type of external connection. Standard account passwords are often insufficient for this role unless special permissions are granted.

The solution lies not in changing your actual email password, but in generating a specific App Password for the application connecting to the server. This acts as a unique, revocable password specifically for that single application, bypassing some of the stricter security checks imposed by Google.

Step-by-Step Solution: Implementing App Passwords

To successfully use Gmail SMTP with Nodemailer, you must generate an App Password instead of using your regular account password.

1. Generating the App Password in Google Account Settings

Follow these steps to create the necessary credential:

  1. Log into your main Google Account.
  2. Navigate to the Security section.
  3. Find the Signing in to Google section (this might be labeled differently depending on interface updates).
  4. If 2-Step Verification is enabled, you must generate an App Password. This process allows you to create a password specifically for applications that don't support modern OAuth flows directly.
  5. Generate a new 16-character password for this specific application. This generated password is what you will use in your Node.js code, not your regular Gmail password.

2. Updating Your Nodemailer Configuration

Once you have the generated App Password, update your Node.js configuration to use it:

var nodemailer = require('nodemailer');

// IMPORTANT: Use the generated App Password here, not your main account password
var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'your_full_email@gmail.com', // Your full Gmail address
        pass: 'YOUR_GENERATED_APP_PASSWORD' // Use the App Password here!
    }
});

var mailOptions = {
    from: 'sender@gmail.com', 
    to: 'recipient@example.com',
    subject: 'Password Reset',
    html: 'Your one time password is : <b>' + temporaryPassword + '</b>'
};

transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
        console.log('Error sending email:', error);
    } else {
        console.log('Email sent successfully:', info.response);
    }
});

Best Practices for Production Email Sending

While App Passwords resolve the immediate issue with Gmail SMTP, relying on personal accounts for high-volume or critical application emails is generally not recommended. For production environments, consider using dedicated Transactional Email Services.

Services like SendGrid, Mailgun, or AWS SES handle the complexity of deliverability, IP reputation, and authentication far better than a direct connection to a consumer email provider. This approach provides better tracking and reliability, which aligns with architectural principles we discuss when building robust systems, much like those encouraged by frameworks such as Laravel.

Conclusion

The failure to send emails via Gmail SMTP is almost always an authentication hurdle related to Google's security policies. By understanding the requirement for App Passwords, developers can unlock access to services like Nodemailer successfully. Always prioritize security and reliability in your application design, ensuring that your email infrastructure is both functional and robust.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.