gen_smtp_client (gen_smtp v1.3.1-1)
View SourceA simple SMTP client used for sending mail - assumes relaying via a smarthost.
Summary
Types
server's 4xx response
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
-type callback() :: fun(({exit, any()} | smtp_session_error() | {ok, binary()}) -> any()).
-type email() :: {From :: email_address(), To :: [email_address(), ...], Body :: string() | binary() | fun(() -> string() | binary())}.
-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()}}.
-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()}}.
-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}].
-type permanent_failure_reason() :: binary() | auth_failed | ssl_not_started.
server's 4xx response
-opaque smtp_client_socket()
-type smtp_host() :: inet:hostname().
-type smtp_session_error() :: {error, no_more_hosts | send, {permanent_failure, smtp_host(), permanent_failure_reason()}} | {error, retries_exceeded | send, host_failure()}.
-type temporary_failure_reason() :: binary() | tls_failed.
-type validate_options_error() :: no_relay | invalid_port | no_credentials.
Functions
-spec close(Socket :: smtp_client_socket()) -> ok.
Close an open smtp client socket opened with open/1.
-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.
-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.
-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.
-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.
-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}.