gen_smtp_client (gen_smtp v1.3.1-1)

View Source

A simple SMTP client used for sending mail - assumes relaying via a smarthost.

Summary

Functions

Close an open smtp client socket opened with open/1.

Deliver an email on an open smtp client socket. For use with a socket opened with open/1. The socket can be reused as long as the previous call to deliver/2 returned {ok, Receipt}. If it's using LMTP protocol, it will return a list with the delivery response for each address {ok, [{"foo@bar.com", "250 ok"}, {"bar@foo.com", "452 <bar@foo.com> is temporarily over quota"}]}`. If the previous call to deliver/2 returned `{error, FailMsg} and the option {on_transaction_error, reset} was given in the open/1 call, the socket may still be reused.

Open a SMTP client socket with the provided options Once the socket has been opened, you can use it with deliver/2.

Send an email in a non-blocking fashion via a spawned_linked process. The process will exit abnormally on a send failure.

Send an email nonblocking and invoke a callback with the result of the send. The callback will receive either {ok, Receipt} where Receipt is the SMTP server's receipt If it's using LMTP protocol, the callback will receive a list with the delivery response for each address {ok, [{"foo@bar.com", "250 ok"}, {"bar@foo.com", "452 <bar@foo.com> is temporarily over quota"}]}`. identifier, `{error, Type, Message} or {exit, ExitReason}, as the single argument.

Send an email and block waiting for the reply. Returns either a binary that contains If it's using LMTP protocol, it will return a list with the delivery response for each address [{"foo@bar.com", "250 ok"}, {"bar@foo.com", "452 <bar@foo.com> is temporarily over quota"}]`. the SMTP servers receipt or {error, Type, Message} or {error, Reason}.

Types

callback/0

-type callback() :: fun(({exit, any()} | smtp_session_error() | {ok, binary()}) -> any()).

email/0

-type email() ::
          {From :: email_address(),
           To :: [email_address(), ...],
           Body :: string() | binary() | fun(() -> string() | binary())}.

email_address/0

-type email_address() :: string() | binary().

extensions/0

-type extensions() :: [{binary(), binary()}].

failure/0

-type failure() ::
          {temporary_failure, temporary_failure_reason()} |
          {permanent_failure, permanent_failure_reason()} |
          {missing_requirement, auth | tls} |
          {unexpected_response, [binary()]} |
          {network_failure, {error, timeout | inet:posix()}}.

host_failure/0

-type host_failure() ::
          {temporary_failure, smtp_host(), temporary_failure_reason()} |
          {permanent_failure, smtp_host(), permanent_failure_reason()} |
          {missing_requirement, smtp_host(), auth | tls} |
          {unexpected_response, smtp_host(), [binary()]} |
          {network_failure, smtp_host(), {error, timeout | inet:posix()}}.

options/0

-type options() ::
          [{ssl, boolean()} |
           {tls, always | never | if_available} |
           {tls_options, list()} |
           {sockopts, [gen_tcp:connect_option()]} |
           {port, inet:port_number()} |
           {timeout, timeout()} |
           {relay, inet:ip_address() | inet:hostname()} |
           {no_mx_lookups, boolean()} |
           {auth, always | never | if_available} |
           {hostname, string()} |
           {retries, non_neg_integer()} |
           {username, string()} |
           {password, string()} |
           {trace_fun, fun((Fmt :: string(), Args :: [any()]) -> any())} |
           {on_transaction_error, quit | reset} |
           {protocol, smtp | lmtp}].

permanent_failure_reason/0

-type permanent_failure_reason() :: binary() | auth_failed | ssl_not_started.

server's 4xx response

smtp_client_socket/0

-opaque smtp_client_socket()

smtp_host/0

-type smtp_host() :: inet:hostname().

smtp_session_error/0

-type smtp_session_error() ::
          {error, no_more_hosts | send, {permanent_failure, smtp_host(), permanent_failure_reason()}} |
          {error, retries_exceeded | send, host_failure()}.

temporary_failure_reason/0

-type temporary_failure_reason() :: binary() | tls_failed.

validate_options_error/0

-type validate_options_error() :: no_relay | invalid_port | no_credentials.

Functions

close(Socket)

-spec close(Socket :: smtp_client_socket()) -> ok.

Close an open smtp client socket opened with open/1.

deliver(Socket, Email)

-spec deliver(Socket :: smtp_client_socket(), Email :: email()) ->
                 {ok, Receipt :: binary() | [{binary(), binary()}, ...]} | {error, FailMsg :: failure()}.

Deliver an email on an open smtp client socket. For use with a socket opened with open/1. The socket can be reused as long as the previous call to deliver/2 returned {ok, Receipt}. If it's using LMTP protocol, it will return a list with the delivery response for each address {ok, [{"foo@bar.com", "250 ok"}, {"bar@foo.com", "452 <bar@foo.com> is temporarily over quota"}]}`. If the previous call to deliver/2 returned `{error, FailMsg} and the option {on_transaction_error, reset} was given in the open/1 call, the socket may still be reused.

open(Options)

-spec open(Options :: options()) ->
              {ok, SocketDescriptor :: smtp_client_socket()} |
              smtp_session_error() |
              {error, bad_option, validate_options_error()}.

Open a SMTP client socket with the provided options Once the socket has been opened, you can use it with deliver/2.

send(Email, Options)

-spec send(Email :: email(), Options :: options()) -> {ok, pid()} | {error, validate_options_error()}.

Send an email in a non-blocking fashion via a spawned_linked process. The process will exit abnormally on a send failure.

send(Email, Options, Callback)

-spec send(Email :: email(), Options :: options(), Callback :: callback() | undefined) ->
              {ok, pid()} | {error, validate_options_error()}.

Send an email nonblocking and invoke a callback with the result of the send. The callback will receive either {ok, Receipt} where Receipt is the SMTP server's receipt If it's using LMTP protocol, the callback will receive a list with the delivery response for each address {ok, [{"foo@bar.com", "250 ok"}, {"bar@foo.com", "452 <bar@foo.com> is temporarily over quota"}]}`. identifier, `{error, Type, Message} or {exit, ExitReason}, as the single argument.

send_blocking(Email, Options)

-spec send_blocking(Email :: email(), Options :: options()) ->
                       binary() |
                       [{binary(), binary()}, ...] |
                       smtp_session_error() |
                       {error, validate_options_error()}.

Send an email and block waiting for the reply. Returns either a binary that contains If it's using LMTP protocol, it will return a list with the delivery response for each address [{"foo@bar.com", "250 ok"}, {"bar@foo.com", "452 <bar@foo.com> is temporarily over quota"}]`. the SMTP servers receipt or {error, Type, Message} or {error, Reason}.