Introduction to the
Nodemailer is an easy-to-use Node.js mail module (via SMTP, Sendmail, or Amazon SES) that supports Unicode and allows you to use any character set you like.
function
- A single module with zero dependencies – code is easy to audit because there are no dead ends
- Security is a top priority, and no one likes RCE vulnerabilities
- Unicode supports the use of any character, including emojis 💪
- Windows support – You can use NPM installation on Windows, as with other modules, there are no compiled dependencies. It’s free to use from Azure or Windows Boxes
- Use HTML content and plain text instead
- Add attachments to the message
- Image attachment for embedded HTML content – your design will not be blocked
- Secure email delivery using TLS/STARTTLS
- Different modes of transport besides built-in SMTP support
- With signed message DKIM
- Custom plug-in support for processing messages
- SAN OAuth2 authentication mode
- Agents are used for SMTP connections
- ES6 code – No more unintentional memory leaks due to hoisting VAR
- Automatically generate email from the email test account email
use
The installation
npm install nodemailer --save
# or
yarn add nodemailer
Copy the code
use
const nodemailer = require('nodemailer')
// Create an SMTP client configuration object
const transporter = nodemailer.createTransport({
// By default, the supported email services include QQ, 163, 126, iCloud, Hotmail, and Yahoo
service: "QQ".auth: {
// Sender email account
user: '[email protected]'.// The sender's email authorization code should be generated in the sender's email Settings, not the login password of the email
pass: '* * * * * * *'}})// Configure recipient information
const receiver = {
// Sender email 'nickname < sender email >'
from: 'Zhang Dali' <[email protected]> './ / theme
subject: 'Acceptance notice'.// The recipient's email can be other email, not necessarily qq email
to: '[email protected]'.// You can use HTML tags
html:
139 1xxx 5xxx
400 0xx 5xxx
www.moyu.com
}
// Send an email
transporter.sendMail(receiver, (error, info) = > {
if (error) {
return console.log('Send failed :', error);
}
transporter.close()
console.log('Sent successfully :', info.response)
})
Copy the code
Send the effect
Configuring the Company Email address
Generally, the company email address is mail.company.com, which is not supported by default. Therefore, you need to configure it separately. Here we use the enterprise wechat mailbox as an example.
const config = {
// Configure the sending server and port number provided by the service provider
host: "smtp.exmail.qq.com".port: '465'.auth: {
// Sender email account
user: '[email protected]'.// The sender's email authorization code should be generated in the sender's email Settings, not the login password of the email
pass: '* * * * * * *'}}const transporter = nodemailer.createTransport(config)
// Send the same method as above
/ /...
Copy the code