izinin / erlang_postmarkapp

An erlang library for consuming the Postmark mail service api

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

erlang_postmarkapp

An Erlang library for consuming the Postmark mail service API, inspired by the official Postmark PHP package https://github.com/wildbit/postmark-php.

IMPORTANT NOTICE

  • JSON dependecy library for this application was replaced and the code implementation has been updated accordingly to support new library API. jsx has been changed to jiffy because jsx does not exist in ejabberd standard installation. In contrast to jsx:encode and decode that accepts proplists , jiffy accepts tuple, i.e. {proplists}
  • Jiffy reference

$ rebar3 compile

To use erlang_postmarkapp from the console, simply do this:

$ ./init.sh

This will build it and start the Erlang shell.

This library currently supports sending emails, handling bounces and managing servers. [are more functions in the planning?]

Add it to your rebar.config like so:

{deps, [
       ...
       {erlang_postmarkapp, "1.*", {git, "https://github.com/emmanix2002/erlang_postmarkapp.git", {branch, "master"}}}
]}.

deliveryStat() = {inactive_mails, integer()} | {bounces, [#postmark_bounce_stats{}]}

deliveryStatsResponse() = [deliveryStat()] | {error, binary()}

bounce() = #postmark_bounce{}

bounces() = [bounce()]

bouncesInfo() = {total, integer()} | {bounces, bounces()}

bouncesResponse() = [bouncesInfo()]

emailBody() = {text, binary()} | {html, binary()}

optionalValue() = binary() | undefined

optionalListValue() = list() | undefined

postmarkEmail() = #postmark_email{}

postmarkEmails() = [postmarkEmail()]

sendEmailResponse() = {ok, binary()} | {error, binary()}

serverColor() = purple | blue | turqoise | green | red | yellow | grey

tag() = binary()

tags() = [tag()]

trackLinkStatus() = none | html_and_text | html_only | text_only

-record(postmark_email, { from :: binary(), to :: binary(), subject :: binary(), html :: optionalValue(), text :: optionalValue(), tag :: optionalValue(), track_opens=true :: boolean(), reply_to :: optionalValue(), cc :: optionalValue(), bcc :: optionalValue(), track_links :: trackLinkStatus(), template_id :: optionalValue(), template_model :: optionalListValue(), inline_css=true :: boolean() }) -record(postmark_send_response, { message_id :: binary(), error_code :: integer(), message :: binary() }) -record(postmark_bounce_request, { count=500 :: integer(), offset=0 :: integer(), bounce_type :: binary(), email :: binary(), inactive :: boolean(), tag :: binary(), message_id :: binary(), from_date :: binary(), to_date :: binary() }) -record(postmark_bounce_stats, { type :: optionalValue(), name :: binary(), count :: integer() }) -record(postmark_bounce, { id :: integer(), type :: binary(), type_code :: integer(), name :: binary(), tag :: optionalValue(), message_id :: optionalValue(), server_id :: optionalValue(), description :: optionalValue(), details :: optionalValue(), email :: binary(), from :: binary(), bounced_at :: binary(), dump_available :: boolean(), inactive :: boolean(), can_activate :: boolean(), subject :: binary() }) -record(postmark_server, { id :: integer(), name :: binary(), api_tokens=[] :: list(), server_link :: binary(), color :: serverColor(), smtp_api_activated :: boolean(), raw_email_enabled :: boolean(), delivery_hook_url :: binary(), inbound_address :: binary(), inbound_hook_url :: binary(), bounce_hook_url :: binary(), include_bounce_content_in_hook :: boolean(), open_hook_url :: boolean(), post_first_open_only :: boolean(), track_opens :: boolean(), track_links :: trackLinkStatus(), inbound_domain :: binary(), inbound_hash :: binary(), inbound_spam_threshold :: integer() })

setup/1, setup/2, send_email/1, send_email/4, send_email/11, send_email_with_template/1, send_email_with_template/10,
send_email_batch/1, get_server_token/0, get_account_token/0, track_links_to_string/1

activate_bounce/1, get_delivery_stats/0, get_bounces/1, get_bounce/1, get_bounce_dump/1, get_bounce_tags/0,

get_server/0, edit_server/1

activate_bounce(BounceId::integer()) -> {ok, bounce()} | {error, binary()}

get_bounces(BounceRequestRecord::#postmark_bounce_request{}) -> bouncesResponse() | {error, binary()}

get_bounce(BounceId::integer()) -> {ok, bounce()} | {error, binary()}

get_bounce_dump(BounceId::integer()) -> {ok, binary()} | {error, binary()}

get_bounce_tags() -> {ok, tags()} | {error, binary()}

get_delivery_stats() -> deliveryStatsResponse()

get_server() -> {ok, #postmark_server{}} | {error, binary()}

edit_server(ServerRecord::#postmark_server{}) -> {ok, #postmark_server{}} | {error, binary()}

setup(ServerToken::binary()) -> ok

setup(ServerToken::binary(), AccountToken::binary()) -> ok

get_server_token() -> binary()

get_account_token() -> binary()

send_email(PostmarkEmail::#postmark_email{}) -> sendEmailResponse()

send_email(From::binary(), To::binary(), Subject::binary(), Body::emailBody()) -> sendEmailResponse()

send_email(From::binary(), To::binary(), Subject::binary(), HtmlBody::optionalValue(), TextBody::optionalValue(), Tag::binary(), TrackOpens::boolean(), ReplyTo::optionalValue(), Cc::optionalValue(), Bcc::optionalValue(), TrackLinks::trackLinkStatus()) -> sendEmailResponse()

send_email_batch(PostmarkEmailList::postmarkEmails()) -> sendEmailResponse()

send_email_with_template(PostmarkEmail::#postmark_email{}) -> sendEmailResponse()

send_email_with_template(From::binary(), To::binary(), TemplateId::binary(), TemplateModel::list(), Tag:: optionalValue(), TrackOpens::boolean(), ReplyTo::optionalValue(), Cc::optionalValue(), Bcc:: optionalValue(), TrackLinks::trackLinkStatus()) -> sendEmailResponse()

track_links_to_string(TrackLinkStatus::trackLinkStatus()) -> binary()

Set up erlang_postmarkapp by providing the Server Token [and optionally an Account Token].
Before you start making calls, you must call the setup/1 or setup/2 functions to initialise the library and start all required processes. We recommend putting the call to setup() in your init function, if you have any.

Here's a simple example of sending a plaintext email:

send_text_mail() ->
    ServerToken = "your server token here",
    erlang_postmarkapp:setup(ServerToken),
    Body = {text, "Hello World!"},
    case erlang_postmarkapp:send_email("signature@domain.com", "recipient@example.com", "A good example", Body) of
        {ok, MessageId} -> lager:info("Successfully sent email with MessageID ~p~n", [MessageId]);
        {error, Reason} -> lager:info("Message sending failed because ~p~n", [Reason])
    end.
  • setup(ServerToken): sets the server token for authenticating with the Postmark API.
  • send_email*(): these functions allow you to send an email.

NOTE: based on the information on the Postman documentation, batch emails are limited to a maximum of 500 messages (the library enforces this). For single emails, you can have a maximum of 50 email addresses from a combination of all To, Cc, and Bcc addresses.

About

An erlang library for consuming the Postmark mail service api

License:Other


Languages

Language:Erlang 99.8%Language:Shell 0.2%