Karta.SMTP.Client (Karta v1.0.0)

The client side of SMTP (RFC 5321) over one connection, for outbound delivery. It speaks the protocol and nothing more: what a reply means for a message is decided by the caller (Karta.Outbound.Transfer).

Every function that talks to the server returns {:ok, client, reply} with the server's Karta.SMTP.Reply, whatever its code, or {:error, reason} when the connection failed: :timeout, :closed, :bad_reply (not an SMTP reply), :reply_too_long, {:tls, reason} or a socket error such as :econnrefused. After an error the connection is closed.

Timeouts (milliseconds; default_timeouts/0, override any with timeouts: %{...}) follow RFC 5321 §4.5.3.2: connect (30 s), greeting (5 min), ehlo (5 min), starttls (2 min, reply and handshake), mail (5 min), rcpt (5 min), data (the 354, 2 min), data_end (the reply after the final dot, 10 min), send (one write of the message; 3 min per data block) and quit (10 s). A read timeout is a deadline for the whole reply, however the server spreads it over packets.

Replies are read with bounds: a line longer than 2048 octets (:reply_too_long; RFC 5321 §4.5.3.1.5 allows 512), more than 100 lines, lines with different codes, or a line that does not start with a code (:bad_reply) end the connection. The enhanced status code (RFC 3463) of the first line is recognised when its class matches the reply code; it is removed from the text of every line that carries it.

After a successful STARTTLS the extensions are forgotten and anything the server sent behind its 220 is discarded (RFC 3207 §4.2): the caller must send EHLO again.

Summary

Functions

Closes the connection.

Sends one command line and reads the reply, with the timeout named by timeout (a key of the timeouts). Raises ArgumentError if the line contains a CR or LF.

Connects to ip and port and reads the greeting. The greeting is returned whatever its code; a 220 means the server is ready (RFC 5321 §3.1).

Sends DATA and, after 354, the message with dot-stuffing (RFC 5321 §4.5.2) and the final <CRLF>.<CRLF>; returns the server's reply to the message. A reply other than 354 to DATA is returned as it is. The message must have CRLF line endings; one is added at the end if missing.

The default timeouts, in milliseconds.

Sends EHLO. On a 2xx reply the extensions it lists are kept (extension/2).

The parameters the last EHLO listed with keyword ("" when none), or nil.

True when the last EHLO listed keyword (case-insensitive).

Sends HELO (for servers that refuse EHLO). No extensions.

The local address and port of the connection.

Sends MAIL FROM:<reverse_path> with params, a list of {keyword, value} or {keyword, true} (written as the bare keyword). "" is the null reverse path.

Sends QUIT, waits briefly for the reply and closes the connection.

Sends RCPT TO:<forward_path> with params (as for mail/3).

Sends RSET.

Sends STARTTLS and, on 220, does the TLS handshake with ssl_options (:ssl client options). Another reply comes back unchanged and the connection stays plain; a failed handshake is {:error, {:tls, reason}}.

Types

result()

@type result() :: {:ok, t(), Karta.SMTP.Reply.t()} | {:error, term()}

t()

@type t() :: %Karta.SMTP.Client{
  buffer: binary(),
  extensions: %{required(String.t()) => String.t()},
  peer: {:inet.ip_address(), :inet.port_number()},
  socket: :gen_tcp.socket() | :ssl.sslsocket(),
  timeouts: %{required(atom()) => non_neg_integer()},
  tls: nil | %{version: String.t(), cipher: String.t()},
  transport: :gen_tcp | :ssl
}

Functions

close(client)

@spec close(t()) :: :ok

Closes the connection.

command(client, line, timeout)

@spec command(t(), String.t(), atom()) :: result()

Sends one command line and reads the reply, with the timeout named by timeout (a key of the timeouts). Raises ArgumentError if the line contains a CR or LF.

connect(ip, port, opts \\ [])

@spec connect(:inet.ip_address(), :inet.port_number(), keyword()) :: result()

Connects to ip and port and reads the greeting. The greeting is returned whatever its code; a 220 means the server is ready (RFC 5321 §3.1).

Options: :timeouts; :source - the local address the connection leaves from (by default the system chooses). It must be an address of this node ({:error, :eaddrnotavail} otherwise) and of the same family as ip ({:error, :source_family} otherwise).

data(client, message)

@spec data(
  t(),
  binary()
  | {:chunks,
     (acc, (binary(), acc -> {:ok, acc} | {:error, term()}) -> result())}
) :: result()
when acc: term()

Sends DATA and, after 354, the message with dot-stuffing (RFC 5321 §4.5.2) and the final <CRLF>.<CRLF>; returns the server's reply to the message. A reply other than 354 to DATA is returned as it is. The message must have CRLF line endings; one is added at the end if missing.

The message is either the bytes themselves or {:chunks, fold}, which reads them as they are sent — a stored body, decrypted and authenticated chunk by chunk — so that a message of any size is sent with the memory of one chunk (Karta.SMTP.Client.Stuff). fold is called with an accumulator and a function to give each piece of the message to, and returns {:ok, acc} or {:error, reason}:

Client.data(client, {:chunks, fn acc, emit -> Messages.stream(message, acc, emit) end})

A bare CR or LF is {:error, :bare_line_ending}: some servers end a message on <LF>.<LF>, which would let message text be read as commands (SMTP smuggling). For the bytes themselves this is found before DATA is sent; for {:chunks, fold} the piece that holds one ends the transaction, and the connection is closed without the final dot, so the server keeps nothing.

default_timeouts()

@spec default_timeouts() :: %{required(atom()) => non_neg_integer()}

The default timeouts, in milliseconds.

ehlo(client, name)

@spec ehlo(t(), String.t()) :: result()

Sends EHLO. On a 2xx reply the extensions it lists are kept (extension/2).

extension(client, keyword)

@spec extension(t(), String.t()) :: String.t() | nil

The parameters the last EHLO listed with keyword ("" when none), or nil.

extension?(client, keyword)

@spec extension?(t(), String.t()) :: boolean()

True when the last EHLO listed keyword (case-insensitive).

helo(client, name)

@spec helo(t(), String.t()) :: result()

Sends HELO (for servers that refuse EHLO). No extensions.

local_address(client)

@spec local_address(t()) ::
  {:ok, {:inet.ip_address(), :inet.port_number()}} | {:error, term()}

The local address and port of the connection.

mail(client, reverse_path, params \\ [])

@spec mail(t(), String.t(), [{String.t(), String.t() | true}]) :: result()

Sends MAIL FROM:<reverse_path> with params, a list of {keyword, value} or {keyword, true} (written as the bare keyword). "" is the null reverse path.

quit(client)

@spec quit(t()) :: :ok

Sends QUIT, waits briefly for the reply and closes the connection.

rcpt(client, forward_path, params \\ [])

@spec rcpt(t(), String.t(), [{String.t(), String.t() | true}]) :: result()

Sends RCPT TO:<forward_path> with params (as for mail/3).

rset(client)

@spec rset(t()) :: result()

Sends RSET.

starttls(client, ssl_options)

@spec starttls(t(), [:ssl.tls_client_option()]) :: result()

Sends STARTTLS and, on 220, does the TLS handshake with ssl_options (:ssl client options). Another reply comes back unchanged and the connection stays plain; a failed handshake is {:error, {:tls, reason}}.