The SMTP Server library provides comprehensive TLS/SSL support to secure your email server communications. This guide covers STARTTLS upgrades, SNI (Server Name Indication) for multi-domain certificates, and certificate configuration.
STARTTLS allows clients to upgrade an insecure connection to a secure one. This is the most common TLS mode for SMTP servers.
1
Enable STARTTLS
Configure your server to support STARTTLS by providing TLS credentials:
const { SMTPServer } = require('smtp-server');const fs = require('fs');const server = new SMTPServer({ secure: false, // Start with insecure connection // Provide TLS certificate and key key: fs.readFileSync('private-key.pem'), cert: fs.readFileSync('certificate.pem'), // Optional: CA certificates for client verification ca: [fs.readFileSync('ca-cert.pem')], onData(stream, session, callback) { stream.pipe(process.stdout); stream.on('end', callback); }});server.listen(587);
2
Verify TLS upgrade
Check the session object to confirm TLS is active:
const server = new SMTPServer({ onData(stream, session, callback) { // Check if connection is secure if (session.secure) { console.log('Connection is encrypted'); console.log('TLS cipher:', session.tlsOptions.name); console.log('Protocol:', session.tlsOptions.version); } stream.on('end', callback); }});
3
Configure TLS options
Customize TLS behavior with advanced options:
const server = new SMTPServer({ key: fs.readFileSync('private-key.pem'), cert: fs.readFileSync('certificate.pem'), // Honor cipher order from server honorCipherOrder: true, // Minimum TLS version (default: TLSv1) minVersion: 'TLSv1.2', // Request OCSP stapling requestOCSP: true, // Require client certificate requestCert: true, rejectUnauthorized: false // Don't reject unauthorized clients});
For advanced SNI handling, provide a custom SNICallback:
const server = new SMTPServer({ key: fs.readFileSync('default-key.pem'), cert: fs.readFileSync('default-cert.pem'), SNICallback(servername, callback) { // Load certificate based on servername console.log('SNI request for:', servername); // Return appropriate secure context // Return null/undefined to use default certificate callback(null, null); }});
The SNICallback is automatically created if not provided. It uses the secureContext map populated from sniOptions. Only override if you need custom logic.
You can update TLS certificates without restarting the server:
const server = new SMTPServer({ key: fs.readFileSync('private-key.pem'), cert: fs.readFileSync('certificate.pem')});server.listen(587);// Later, update the certificatesserver.updateSecureContext({ key: fs.readFileSync('new-private-key.pem'), cert: fs.readFileSync('new-certificate.pem'), // Update SNI options too sniOptions: { 'mail.example.com': { key: fs.readFileSync('new-example-key.pem'), cert: fs.readFileSync('new-example-cert.pem') } }});
The updateSecureContext() method is called automatically during initialization. Existing connections are not affected; only new connections use the updated certificates.