I’m trying to set up email sending using Nodemailer and Gmail’s SMTP service but I’m running into some issues. Here’s what I’ve done so far:
const emailSender = nodemailer.createTransport(smtpPool({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'mypassword'
},
maxConnections: 5,
maxMessages: 10
}));
const emailContent = {
from: sender,
to: '[email protected]',
subject: emailSubject,
text: emailBody,
html: emailBody
};
emailSender.sendMail(emailContent, (err, result) => {
if (err) {
console.error(err);
} else {
console.log('Email sent successfully');
}
});
When I run this code, I get an error saying ‘Invalid login’ with the code ‘EAUTH’. The weird thing is, I can log into my Gmail account just fine using the same password in my web browser. Any ideas what might be causing this? Is there something special I need to do to use Gmail with Nodemailer?
I encountered this exact problem when setting up email notifications for my side project. The solution that worked for me was using OAuth2 authentication instead of regular SMTP credentials. Here’s what I did:
- Set up OAuth2 credentials in the Google Cloud Console.
- Install the ‘googleapis’ package.
- Modify the Nodemailer transport configuration to use OAuth2.
It’s a bit more complex to set up initially, but it’s much more secure and reliable in the long run. Plus, you avoid the headaches of dealing with Gmail’s changing security policies for less secure apps.
If you’re interested, I can share some code snippets showing how I implemented this. Just let me know, and I’ll be happy to provide more details.
hey there, i had similar issues. gmail’s security is pretty tight now. you need to enable ‘less secure app access’ in your google account settings or use an ‘app password’ instead of your regular one. also, make sure you’re using the latest nodemailer version. hope this helps!
I’ve faced this issue before, and it’s usually related to Google’s security measures. Instead of using your regular password, try generating an app-specific password for your application. Go to your Google Account settings, navigate to the Security section, and look for ‘App passwords’. Create a new one for your Node.js app and use that in your code.
Also, ensure you’re using ‘OAuth2’ instead of regular SMTP. It’s more secure and less likely to trigger Google’s security flags. You’ll need to set up OAuth2 credentials in the Google Cloud Console and modify your Nodemailer configuration accordingly.
If you’re still having trouble, double-check that your Gmail account doesn’t have two-factor authentication enabled, as this can complicate things further.