z_email_server (zotonic_core v1.0.0-rc.17)

Email server. Queues, renders and sends e-mails.

Summary

Functions

Handle a bounce

Convert process state when code is changed

Handle a delivery report from an outside service like mailgun

Generate a new message id

Send an e-mail.

Poll the database queue for any retrys.

Initiates the server.

Check if the received e-mail address is a bounce address

Check if the sender is allowed to send email. Disabled users are only allowed to send mail to themselves or to the admin.

Check if a file is a tempfile of the emailer

Return the max age of a tempfile

Force a poll to send new email

Generate a reply email address for the message-id. Used when reply_to in the email record is set to 'message_id'. Returns an RFC822-formatted mailbox using the address "reply+MessageId@site_email_domain" (optionally with a display-name).

Send an email, generate an unique message-id for it.

Send an email using a predefined unique id. Emails are only sent for enabled senders and non-backup environments.

Starts the server

Return the filename for a tempfile that can be used for the emailer

This function is called by a gen_server when it is about to terminate. It should be the opposite of Module:init/1 and do any necessary cleaning up. When it returns, the gen_server terminates with Reason. The return value is ignored.

Types

delivery_type/0

-type delivery_type() :: permanent_failure | temporary_failure | sent | received | relayed.

Functions

bounced(Peer, NoReplyEmail)

Handle a bounce

code_change(OldVsn, State, Extra)

-spec code_change(term(), term(), term()) -> {ok, term()}.

Convert process state when code is changed

delivery_report(What, OptRecipient, MsgIdHeader, OptStatusMessage)

-spec delivery_report(delivery_type(), binary() | undefined, binary(), binary() | undefined) -> ok.

Handle a delivery report from an outside service like mailgun

generate_message_id()

-spec generate_message_id() -> binary().

Generate a new message id

get_email_from(Context)

-spec get_email_from(z:context()) -> binary().

handle_call(Message, From, State)

handle_cast(Message, State)

-spec handle_cast(term(), term()) ->
                     {noreply, term()} |
                     {noreply, term(), timeout() | hibernate} |
                     {stop, term(), term()}.

Send an e-mail.

handle_info(Info, State)

-spec handle_info(term(), term()) ->
                     {noreply, term()} |
                     {noreply, term(), timeout() | hibernate} |
                     {stop, term(), term()}.

Poll the database queue for any retrys.

init(Args)

-spec init(term()) -> {ok, term()} | {ok, term(), timeout() | hibernate} | ignore | {stop, term()}.

Initiates the server.

is_bounce_email_address(_)

Check if the received e-mail address is a bounce address

is_recipient_ok(Recipient, Context)

is_sender_enabled(Email, Context)

Check if the sender is allowed to send email. Disabled users are only allowed to send mail to themselves or to the admin.

is_sender_enabled(Id, RecipientEmail, Context)

is_tempfile(File)

Check if a file is a tempfile of the emailer

is_tempfile_deletable(File)

Return the max age of a tempfile

poll()

-spec poll() -> ok.

Force a poll to send new email

reply_to_message_id(MessageId, Context)

-spec reply_to_message_id(MessageId, Context) -> ReplyToEmail
                             when
                                 MessageId :: binary(), Context :: z:context(), ReplyToEmail :: binary().

Generate a reply email address for the message-id. Used when reply_to in the email record is set to 'message_id'. Returns an RFC822-formatted mailbox using the address "reply+MessageId@site_email_domain" (optionally with a display-name).

send(Email, Context)

-spec send(Email, Context) -> {ok, SentEmailId} | {error, Reason}
              when
                  Email ::
                      #email{to :: list() | string() | binary() | m_rsc:resource_id() | undefined,
                             cc :: list() | string() | binary() | undefined,
                             bcc :: list() | string() | binary() | undefined,
                             from :: binary() | string(),
                             reply_to :: binary() | string() | message_id | undefined,
                             headers :: [{binary(), binary()}],
                             body :: term(),
                             raw :: term(),
                             subject :: iodata() | undefined,
                             text :: iodata() | undefined,
                             html :: iodata() | undefined,
                             text_tpl :: template_compiler:template() | undefined,
                             html_tpl :: template_compiler:template() | undefined,
                             vars :: list(),
                             attachments :: list(),
                             queue :: boolean()},
                  SentEmailId :: binary(),
                  Context :: z:context(),
                  Reason :: env_backup | sender_disabled.

Send an email, generate an unique message-id for it.

send(EmailId, Email, Context)

-spec send(EmailId, Email, Context) -> {ok, SentEmailId} | {error, Reason}
              when
                  EmailId :: binary() | string(),
                  Email ::
                      #email{to :: list() | string() | binary() | m_rsc:resource_id() | undefined,
                             cc :: list() | string() | binary() | undefined,
                             bcc :: list() | string() | binary() | undefined,
                             from :: binary() | string(),
                             reply_to :: binary() | string() | message_id | undefined,
                             headers :: [{binary(), binary()}],
                             body :: term(),
                             raw :: term(),
                             subject :: iodata() | undefined,
                             text :: iodata() | undefined,
                             html :: iodata() | undefined,
                             text_tpl :: template_compiler:template() | undefined,
                             html_tpl :: template_compiler:template() | undefined,
                             vars :: list(),
                             attachments :: list(),
                             queue :: boolean()},
                  SentEmailId :: binary(),
                  Context :: z:context(),
                  Reason :: env_backup | sender_disabled.

Send an email using a predefined unique id. Emails are only sent for enabled senders and non-backup environments.

start_link()

-spec start_link() -> {ok, pid()} | ignore | {error, term()}.

Starts the server

tempfile()

Return the filename for a tempfile that can be used for the emailer

terminate(Reason, State)

-spec terminate(term(), term()) -> ok.

This function is called by a gen_server when it is about to terminate. It should be the opposite of Module:init/1 and do any necessary cleaning up. When it returns, the gen_server terminates with Reason. The return value is ignored.