emailto
v1.0.3
Published
Type-safe, simple npm library for sending emails via an SMTP server.
Maintainers
Readme
emailto
Send emails via SMTP. Simple, typed, no nonsense.
npm install emailtoimport { Emailto } from "emailto";
const mailer = new Emailto({
host: "smtp.gmail.com",
port: 465,
auth: { user: "[email protected]", pass: "app-password" },
});
await mailer.send({
from: "[email protected]",
to: "[email protected]",
subject: "Hey",
text: "Hello there.",
});
mailer.close();Config
| Option | Default | Description |
|---------------------|----------------|---------------------------------|
| host | — | SMTP hostname |
| port | — | 465 · 587 · 25 |
| secure | port === 465 | TLS/SSL |
| auth | — | { user, pass } |
| connectionTimeout | 30_000 | ms |
| greetingTimeout | 30_000 | ms |
| socketTimeout | 60_000 | ms |
| allowSelfSigned | false | Trust self-signed certs |
Message
| Field | Required | Description |
|---------------|----------|-----------------------------------------|
| from | ✓ | "addr" or { name, address } |
| to | ✓ | One or many recipients |
| cc / bcc | | One or many |
| replyTo | | |
| subject | ✓ | |
| text | ✓* | Plain-text body |
| html | ✓* | HTML body |
| attachments | | { filename, path } or { filename, content } |
| headers | | Record<string, string> |
* At least one of
textorhtmlrequired.
Methods
await mailer.verify(); // test connection & credentials
await mailer.send(message);
mailer.close(); // free TCP connection when doneErrors
Every error extends EmailToError with a code string and optional cause.
import {
Emailto,
ValidationError,
AuthError,
ConnectionError,
TimeoutError,
AllRecipientsRejectedError,
} from "emailto";
try {
await mailer.send({ ... });
} catch (err) {
if (err instanceof ValidationError) console.error(err.field, err.message);
if (err instanceof AuthError) console.error("bad credentials");
if (err instanceof ConnectionError) console.error("unreachable");
if (err instanceof TimeoutError) console.error("timed out");
}| Code | Class |
|---------------------------|------------------------------|
| MISSING_SENDER | ValidationError |
| MISSING_RECIPIENT | ValidationError |
| MISSING_SUBJECT | ValidationError |
| MISSING_BODY | ValidationError |
| INVALID_ADDRESS | ValidationError |
| ATTACHMENT_NOT_FOUND | ValidationError |
| AUTH_FAILED | AuthError |
| CONNECTION_FAILED | ConnectionError |
| CONNECTION_TIMEOUT | TimeoutError |
| SEND_FAILED | SendError |
| ALL_RECIPIENTS_REJECTED | AllRecipientsRejectedError |
| INVALID_CONFIG | ConfigError |
MIT
