In the Nodemailer Tutorial, I have covered how to send emails with Node.js. Nodemailer is a powerful module for Node.js applications to allow easy emailing capabilities. In this nodemailer tutorial, we’ll explore how to send emails in Node.js using multiple examples.
Table of Contents
- Setting Up Nodemailer
- Sending a Basic Email
- Sending Emails with Attachments
- Sending HTML Emails
- Using Custom SMTP Providers
- Troubleshooting Tips
- Conclusion
- References
Setting Up Nodemailer
To start sending emails using Node.js, first install the Nodemailer package using npm:
npm install nodemailer
Next, include the Nodemailer module in your script:
const nodemailer = require('nodemailer');
Sending a Basic Email
Create a transporter object using the default SMTP transport:
const transporter = nodemailer.createTransport({ service: 'Gmail', auth: { user: '[email protected]', pass: 'your-password' } });
Send an email with the following content:
let mailOptions = { from: '[email protected]', to: '[email protected]', subject: 'Hello', text: 'Hello world!' }; transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.log(error); } console.log('Message sent: %s', info.messageId); });
Output: Message sent: [messageId]
Sending Emails with Attachments
To send email with attachments, include the attachments in the mailOptions object:
mailOptions = { // previous mailOptions settings... attachments: [ { filename: 'textfile.txt', content: 'Hello world!' }, { path: '/path/to/file.pdf' } ] }; // rest of the sendMail function...
Output: Message sent: [messageId] with attachments
Sending HTML Emails
For a more styled email, you can use HTML content:
mailOptions = { // previous mailOptions settings... html: '<b>Hello world!</b>' }; // rest of the sendMail function...
Output: Message sent: [messageId] with HTML content
Using Custom SMTP Providers
To use a custom SMTP server, configure the transporter object accordingly:
const customTransporter = nodemailer.createTransport({ host: 'smtp.example.com', port: 465, secure: true, auth: { user: 'username', pass: 'password' } });
Send the mail using the custom transporter:
// Define mailOptions as before... customTransporter.sendMail(mailOptions, (error, info) => { // Handling error and info as before... });
If you want to generate a MIME message and send email using the Gmail API, follow this.
Troubleshooting Tips
Occasionally, sending an email might not work as expected. Here are some tips:
- Check if the username and password are correct.
- Ensure your email service provider allows connections from unknown apps.
- Verify if the SMTP server is correct and that the port and security settings align with your email service provider’s requirements.
- Inspect error messages for clues.
Conclusion
We’ve gone through examples ranging from sending a basic text email to using HTML content and sending attachments with custom SMTP configurations. Nodemailer provides an effective way for Node.js applications to interact with various email services, making it indispensable for applications that need such functionalities.