In this article, you'll learn how to send emails using the nodemailer module. Between the most know features of nodemailer are:
- Node.js 0.10+, no ES6 shenanigans used that would break your production app.
- Unicode to use any characters, including full emoji support.
- Windows – you can install it with npm on Windows just like any other module, there are no compiled dependencies. Use it from Azure or from your Windows box hassle free.
- HTML content as well as plain text alternative.
- Attachments (including attachment streaming for sending larger files).
- Embedded images in HTML.
- Secure e-mail delivery using SSL/STARTTLS.
- Different transport methods, either using built-in SMTP transports or from external plugins.
- Custom plugin support for manipulating messages (add DKIM signatures, use markdown content instead of HTML etc.).
- Sane XOAUTH2 login with automatic access token generation (and feedback about the updated tokens).
- Simple built-in templating using node-email-templates or custom renderer.
- Proxies for SMTP connections (SOCKS, HTTP and custom connections).
Requirements
In order to send emails in Node.js, as mentioned previously, you'll need the nodemailer module. To add nodemailer as a dependency in your project execute the following command in the Node.js command prompt:
You can visit the official Github repository of nodemailer for more information or the package site in NPM here. After the download, you'll be able to require the module using "require('nodemailer')
".
Send with a Gmail account
Google uses SSL encryption and the port 465.
Note: to use Gmail you may need to configure "Allow Less Secure Apps" in your Gmail account unless you are using 2FA in which case you would have to create an Application Specific password. You also may need to unlock your account with "Allow access to your Google account" to use SMTP.
Send with a Zoho account
Zoho mail uses SSL encryption and the port 465, the same as Gmail.
Send with an Outlook (or hotmail) account
Outlook uses TLS encryption in the port 587 unlike Gmail or Zoho. In this case we need to disable the default secure connection to enable TLS using the SSLv3 encryption.
Alternatively, if your account is hotmail instead of outlook, you can use the buil-in hotmail service using the following transport:
Have fun !
5 Comments