Skip to content

smtp

The smtp package sends email through an SMTP server, either in one call or with a fluent Message builder.

smtp::send(
"smtp.example.com", 587,
"sender@example.com", # from
"recipient@example.com", # to: an address or a list of addresses
"Hello from Kiwi", # subject
"This is the message body.", # body
"sender@example.com", "p@ssw0rd" # username and password
)

The builder suits messages with several recipients:

smtp::message("alerts@example.com", "Build failed", "The CI pipeline failed on main.")
.add_to("dev@example.com")
.add_to("ops@example.com")
.add_cc("manager@example.com")
.send("smtp.example.com", 587, "alerts@example.com", "p@ssw0rd")

TLS is on by default and is negotiated with STARTTLS, as on port 587; pass use_tls: false for servers without TLS. Servers that only accept implicit TLS (typically port 465) are not supported.

Keep credentials out of source code: read them with env::get or load them with dotenv. smtp is unavailable in safe mode.

smtp::send(host, port, from_addr, recipients, subject, body, username = "", password = "", use_tls = true)

Sends an email in one call.

ParameterTypeDefaultDescription
hoststringSMTP server hostname.
portintegerSMTP server port.
from_addrstringSender email address.
recipientsstring|listRecipient address or list of addresses.
subjectstringEmail subject.
bodystringEmail body.
usernamestring""SMTP username (default "").
passwordstring""SMTP password (default "").
use_tlsbooleantrueWhether to use TLS (default true).

Returns boolean — true on success.

import smtp
smtp::send("smtp.example.com", 587, "me@example.com", "you@example.com",
"Hello", "This is the body.", "me@example.com", "s3cr3t")
smtp::message(from_addr, subject, body)

Creates a fluent Message builder.

ParameterTypeDefaultDescription
from_addrstringSender email address.
subjectstringEmail subject.
bodystringEmail body.

Returns Message

A fluent email message builder.

Message.new(from_addr, subject, body)
ParameterTypeDefaultDescription
from_addrany
subjectany
bodyany
add_to(addr)

Adds a recipient.

ParameterTypeDefaultDescription
addrstringEmail address.

Returns Message — self

add_cc(addr)

Adds a CC recipient.

ParameterTypeDefaultDescription
addrstringEmail address.

Returns Message — self

send(host, port = 587, username = "", password = "", use_tls = true)

Sends the message via SMTP.

ParameterTypeDefaultDescription
hoststringSMTP server hostname.
portinteger587SMTP server port (default 587).
usernamestring""SMTP username (default "").
passwordstring""SMTP password (default "").
use_tlsbooleantrueWhether to use TLS/STARTTLS (default true).

Returns boolean — true on success.