This error occurs on Node.js applications deployed on Heroku without proper configuration of environment variables. Additionally, you may also run into this error due to invalid configurations.
Resolving the error by updating configurations
Ensure that you have configured Nodemailer successfully. You may use the snippet shown below as a guide for configuring Nodemailer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
const transporter = nodemailer.createTransport({ service: 'gmail', // or your own SMTP providerauth: {user: 'your-gmail@gmail.com'}, // user -> important pass: 'your-password' // pass -> important (do not use password) }); const mailOptions = { from: 'SENDER-EMAIL', to: 'RECIPIENT-EMAIL', subject: 'SUBJECT', text: 'EMAIL BODY', }; transporter.sendMail(mailOptions,(err,res)=>{ if(err){ console.log(err); } else { console.log('The email was sent successfully'); } });
Additionally, if you are using Google SMTP servers, ensure that you have turned on Less Secure Sign In. For users that use Office 365, ensure that SMTP authentication is turned on for your Microsoft account.
Resolving the error in Heroku
You will need to configure your Nodemailer environment variables in the deployed Heroku application.
To do so, first, log in to your Heroku console. Afterward, click on the deployed application and click "Settings." You will see the output shown below.
Figure 01 - Heroku application settings
Hereafter, click on "Reveal Config Vars." It will display the output shown below.
Figure 02 - Viewing config variables in the Heroku console
Then, add the environment variables that you use with Nodemailer.
Figure 03 - Adding config variables to Heroku
Finally, re-rerun the application to see the emails getting sent successfully!
Resolving steps if none of the above works
If none of the actions work for you, ensure that you have provided the correct username/password, port, and protocol. Additionally, if you use dotenv
to load the environment file, ensure that you properly access the configurations.
1 2 3 4 5 6
// add this snippet to the top of your Node.js file to initialize the .env file that you have created to store the env variables. const dotenv = require('dotenv'); dotenv.config(); // access env console.log(process.env.NODE_MAILER_USERNAME);
View all errors
Send up to 10,000 notifications every month, for free.
Get started for free
Send up to 10,000 notifications every month, for free.
Get started for free
© 2024 Courier. All rights reserved.