EmailProvidersSMTP

SMTP

Send emails using the SMTP protocol with @visulima/email

SMTP Provider

The SMTP provider allows you to send emails using the standard SMTP protocol (RFC 5321) with a built-in SMTP client. No external library is required.

Runtime Support: Node.js only (requires node:net and node:tls)

Setup

import { createMail } from "@visulima/email";
import { smtpProvider } from "@visulima/email/providers/smtp";

const mail = createMail(
    smtpProvider({
        host: "smtp.example.com",
        port: 587,
        secure: false,
        user: process.env.SMTP_USER!,
        password: process.env.SMTP_PASSWORD!,
    }),
);

Configuration

The SmtpConfig interface extends BaseConfig with the following options:

OptionTypeRequiredDefaultDescription
hoststringYes-SMTP server hostname
portnumberYes-SMTP server port (25, 465, 587, etc.)
securebooleanNofalseUse TLS (true for port 465)
userstringNo-SMTP username
passwordstringNo-SMTP password
authMethod"LOGIN" | "PLAIN" | "CRAM-MD5" | "OAUTH2"No-Authentication method
poolbooleanNofalseEnable connection pooling
maxConnectionsnumberNo-Maximum pool connections
rejectUnauthorizedbooleanNo-Reject unauthorized TLS certificates
dkimobjectNo-DKIM signing configuration
oauth2objectNo-OAuth2 authentication configuration
debugbooleanNofalseEnable debug logging
loggerConsoleNo-Custom logger instance
retriesnumberNo3Number of retry attempts
timeoutnumberNo30000Request timeout in milliseconds

DKIM Configuration

const mail = createMail(
    smtpProvider({
        host: "smtp.example.com",
        port: 587,
        user: "user",
        password: "pass",
        dkim: {
            domainName: "example.com",
            keySelector: "default",
            privateKey: "-----BEGIN PRIVATE KEY-----\n...",
        },
    }),
);

OAuth2 Authentication

const mail = createMail(
    smtpProvider({
        host: "smtp.gmail.com",
        port: 465,
        secure: true,
        authMethod: "OAUTH2",
        oauth2: {
            user: "user@gmail.com",
            clientId: "client-id",
            clientSecret: "client-secret",
            refreshToken: "refresh-token",
            accessToken: "access-token",
        },
    }),
);

Basic Usage

import { MailMessage } from "@visulima/email";

const message = new MailMessage()
    .to("user@example.com")
    .from("sender@example.com")
    .subject("Hello via SMTP")
    .html("<h1>Hello World</h1>");

const result = await mail.send(message);

Provider-Specific Email Options

The SMTP provider supports additional options through SmtpEmailOptions:

import type { SmtpEmailOptions } from "@visulima/email/providers/smtp";

const options: SmtpEmailOptions = {
    from: { email: "sender@example.com" },
    to: { email: "user@example.com" },
    subject: "Hello",
    html: "<h1>Hello</h1>",
    priority: "high",
    inReplyTo: "<original-message-id@example.com>",
    references: ["<message-1@example.com>", "<message-2@example.com>"],
    listUnsubscribe: "https://example.com/unsubscribe",
    useDkim: true,
    dsn: {
        success: true,
        failure: true,
        delay: false,
    },
    googleMailHeaders: {
        category: "primary",
        feedbackId: "feedback-123",
        promotionalContent: false,
    },
};
OptionTypeDescription
priority"high" | "normal" | "low"Message priority
inReplyTostringReference to a previous message ID (threading)
referencesstring | string[]References to related message IDs
listUnsubscribestring | string[]List-Unsubscribe header
useDkimbooleanSign the email with DKIM
dsnobjectDelivery Status Notification options
googleMailHeadersobjectGoogle Mail-specific headers

Supported Features

FeatureSupported
AttachmentsYes
Batch SendingNo
Custom HeadersYes
HTMLYes
Reply-ToYes
SchedulingNo
TaggingNo
TemplatesNo
TrackingNo
Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues