SMTP client module to connect to SMTP servers and send mail with it.
This module is the successor for the client part of the (now deprecated) SMTP module simplesmtp. For matching SMTP server see smtp-server.
Step 1. Install Nodemailer with npm
smtp-connection is exposed as a submodule of Nodemailer
npm install nodemailer --save
Step 2. Require smtp-connection in your script
const SMTPConnection = require("nodemailer/lib/smtp-connection");
Step 3. Create SMTPConnection instance
let connection = new SMTPConnection(options);
SMTPConnection instances are event emitters with the following events
- ‘error’ (err) emitted when an error occurs. Connection is closed automatically in this case.
- ‘connect’ emitted when the connection is established
- ‘end’ when the instance is destroyed
Establish the connection
- callback is the function to run once the connection is established. The function is added as a listener to the ‘connect’ event.
After the connect event the connection has the following properties:
- connection.secure - if true then the connection uses a TLS socket, otherwise it is using a cleartext socket. Connection can start out as cleartext but if available (or requireTLS is set to true) connection upgrade is tried
If the server requires authentication you can login with
Once the connection is authenticated (or just after connection is established if authentication is not required), you can send mail with
connection.send(envelope, message, callback);
envelope is the envelope object to use
- envelope.from is the sender address
- envelope.to is the recipient address or an array of addresses
- envelope.size is an optional value of the predicted size of the message in bytes. This value is used if the server supports the SIZE extension (RFC1870)
- envelope.use8BitMime if true then inform the server that this message might contain bytes outside 7bit ascii range
- envelope.dsn is the dsn options
- envelope.dsn.ret return either the full message ‘FULL’ or only headers ‘HDRS’
- envelope.dsn.envid sender’s ‘envelope identifier’ for tracking
- envelope.dsn.notify when to send a DSN. Multiple options are OK - array or comma delimited. NEVER must appear by itself. Available options: ‘NEVER’, ‘SUCCESS’, ‘FAILURE’, ‘DELAY’
- envelope.dsn.orcpt original recipient
message is either a String, Buffer or a Stream. All newlines are converted to \r\n and all dots are escaped automatically, no need to convert anything before.
callback is the callback to run once the sending is finished or failed. Callback has the following arguments
- err and error object if sending failed
- err.code string code identifying the error, for example ‘EAUTH’ is returned when authentication fails
- err.response is the last response received from the server (if the error is caused by an error response from the server)
- err.responseCode is the numeric response code of the response string (if available)
- info information object about accepted and rejected recipients
- info.accepted an array of accepted recipient addresses. Normally this array should contain at least one address except when in LMTP mode. In this case the message itself might have succeeded but all recipients were rejected after sending the message.
- info.rejected an array of rejected recipient addresses. This array includes both the addresses that were rejected before sending the message and addresses rejected after sending it if using LMTP
- info.rejectedErrors if some recipients were rejected then this property holds an array of error objects for the rejected recipients
- info.response is the last response received from the server
Use it for graceful disconnect
Use it for less graceful disconnect
Use it to reset current session (invokes RSET command)