SMTP is the main transport in Nodemailer for delivering messages. SMTP is also the protocol used between different email hosts, so it’s truly universal. Almost every email delivery provider supports SMTP-based sending, even if they mainly push their API-based sending. APIs might have more features, but using these also means vendor lock-in. In the case of SMTP, you only need to change the configuration options to replace one provider with another, and you’re good to go.
let transporter = nodemailer.createTransport(options[, defaults])
Where
Alternatively, you could use a connection url instead of an object for the options. Use ‘smtp:' or ‘smtps:' as the protocol in the url.
let poolConfig = "smtps://username:password@smtp.example.com/?pool=true";
Hostnames for the host field are resolved using dns.resolve()
. If you are using a non-resolvable hostname (eg. something listed in /etc/hosts or you are using different resolver for your Node apps), then provide the IP address of the SMTP server as host and use the actual hostname in the tls.servername parameter. This way, no hostname resolving is attempted, but TLS validation still works.
host
was set to an IP addressSetting secure to false does not mean that you would not use an encrypted connection. Most SMTP servers allow connection upgrade via the STARTTLS command, but to use this, you have to connect using plaintext first.
true
then logs to console. If value is not set or is false
then nothing is loggedThis example would connect to a SMTP server separately for every single message
nodemailer.createTransport({
host: "smtp.example.com",
port: 587,
secure: false, // upgrade later with STARTTLS
auth: {
user: "username",
pass: "password",
},
});
This example would set up pooled connections against an SMTP server on port 465.
nodemailer.createTransport({
pool: true,
host: "smtp.example.com",
port: 465,
secure: true, // use TLS
auth: {
user: "username",
pass: "password",
},
});
This config would open a connection to a TLS server with self-signed or invalid TLS certificate.
nodemailer.createTransport({
host: "my.smtp.host",
port: 465,
secure: true, // use TLS
auth: {
user: "username",
pass: "pass",
},
tls: {
// do not fail on invalid certs
rejectUnauthorized: false,
},
});
If authentication data is not present, the connection is considered authenticated from the start. Otherwise you would need to provide the authentication options object.
auth is the authentication object
For authenticating using OAuth2 instead of normal auth, see OAuth2 options for the auth object here.
You can also define custom authentication handlers for protocols that are not natively supported by Nodemailer, see NTLM handler as an example of such custom handler.
You can verify your SMTP configuration with verify(callback) call (also works as a Promise). If it returns an error, then something is not correct, otherwise the server is ready to accept messages.
// verify connection configuration
transporter.verify(function (error, success) {
if (error) {
console.log(error);
} else {
console.log("Server is ready to take our messages");
}
});
Be aware though that this call only tests connection and authentication, but it does not check if the service allows you to use a specific envelope “From” address or not.