nodemailer-smtp-transport

repository·master·Indexed 42 days ago

https://github.com/nodemailer/nodemailer-smtp-transport

An SMTP transport implementation for Nodemailer version 2.7.4 that enables sending emails via the Simple Mail Transfer Protocol. It supports configuration via options objects, connection URLs, and predefined service names, and includes methods for sending mail via send() and verifying configurations via verify().

Tokens
1.2K
Snippets
4
Records
5
Agent score
3%

What's inside nodemailer-smtp-transport

  1. Overview of nodemailer-smtp-transport

    master
    The nodemailer-smtp-transport module provides the SMTP transport implementation for Nodemailer. It allows Nodemailer to send emails via the Simple Mail Transfer Protocol (SMTP). For detailed information on SMTP protocols, terms of use, and general email sending logic, refer to the official Nodemailer SMTP documentation.
  2. Verify SMTP configuration with `verify()`

    master

    The verify(callback) method tests the SMTP configuration by attempting to connect and authenticate. It is useful for checking if your credentials and host settings are correct before attempting to send mail.

    • Callback/Promise support: You can provide a callback function (err, result). If no callback is provided and a Promise-compatible environment is detected, it returns a Promise that resolves to true on success or rejects on error.
    // Using a callback
    transport.verify((err, result) => {
        if (err) {
            return console.log('Verification failed:', err);
        }
        console.log('Server is ready to accept queued messages');
    });
    
    // Using a Promise
    transport.verify()
        .then(() => console.log('Verified!'))
        .catch(err => console.error('Verification failed:', err));
  3. Implement a custom `getSocket` function

    master

    The SMTPTransport allows you to provide a custom getSocket function in the configuration options. This is used to implement proxy sockets or custom connection logic.

    If getSocket is provided, the transport calls it before connecting. If the callback returns a connection object within socketOptions, those options are merged into the SMTP connection settings.

    const transport = smtpTransport({
        host: 'smtp.example.com',
        getSocket: (options, callback) => {
            // Implement custom socket logic here
            // callback(err, socketOptions)
            callback(null, false);
        }
    });
  4. Send an e-mail with `send()`

    master

    The send(mail, callback) method sends an e-mail using the configured SMTP settings.

    • mail: A mail object containing the message and envelope information.
    • callback: A function called with (err, info). If successful, info includes the envelope (from/to) and the messageId.

    The method handles connection establishment, authentication (if auth is configured), and connection closure automatically.

    transport.send(mail, (err, info) => {
        if (err) {
            return console.log('Error occurred:', err);
        }
        console.log('Message sent:', info.messageId);
        console.log('Envelope:', info.envelope);
    });
  5. Create an SMTP transport instance

    master

    To create an SMTP transport, call the exported function with a configuration object or a connection URL string. The transport supports configuration via options or a url property. If a service name is provided, it will automatically pull in 'wellknown' configuration settings for that service.

    Supported configuration methods:

    • Options Object: Pass an object containing connection details (host, port, auth, tls, etc.).
    • Connection URL: Pass a string URL (e.g., smtp://user:pass@host:port).
    • Service Name: Provide a service key to use predefined settings for common providers.
    const smtpTransport = require('nodemailer-smtp-transport');
    
    // Using an options object
    const transport = smtpTransport({
        host: 'smtp.example.com',
        port: 587,
        auth: {
            user: 'user@example.com',
            pass: 'password'
        }
    });
    
    // Using a connection URL
    const transportByUrl = smtpTransport('smtp://user:pass@smtp.example.com:587');