MailerLite v0.3.0 MailerLite.Campaigns View Source
Get, create, delete and manage your email campaigns.
Link to this section Summary
Functions
Cancels a campaign which has outbox status.
Delete a (non scheduled) campaign.
Returns all campaigns you have in your account by status.
Create a new campaign. Returns the new campaign details.
Schedule and send a campaign that has draft status and has step value equal to 3.
Uploads an HTML and plain text template to a campaign.
Link to this section Types
campaign()
View Source
campaign() :: %{
clicked: %{count: non_neg_integer(), rate: non_neg_integer()},
date_created: String.t(),
date_send: String.t(),
id: non_neg_integer(),
name: String.t(),
opened: %{count: non_neg_integer(), rate: non_neg_integer()},
status: String.t(),
total_recipients: non_neg_integer(),
type: String.t()
}
campaign() :: %{
clicked: %{count: non_neg_integer(), rate: non_neg_integer()},
date_created: String.t(),
date_send: String.t(),
id: non_neg_integer(),
name: String.t(),
opened: %{count: non_neg_integer(), rate: non_neg_integer()},
status: String.t(),
total_recipients: non_neg_integer(),
type: String.t()
}
get_options()
View Source
get_options() :: %{
limit: non_neg_integer(),
offset: non_neg_integer(),
order: String.t()
}
get_options() :: %{
limit: non_neg_integer(),
offset: non_neg_integer(),
order: String.t()
}
new_campaign()
View Source
new_campaign() :: %{
groups: [non_neg_integer()],
subject: String.t(),
type: String.t(),
ab_settings: %{
ab_win_type: String.t(),
send_type: String.t(),
split_part: String.t(),
winner_after: non_neg_integer(),
winner_after_type: String.t(),
values: [String.t()]
}
}
new_campaign() :: %{
groups: [non_neg_integer()],
subject: String.t(),
type: String.t(),
ab_settings: %{
ab_win_type: String.t(),
send_type: String.t(),
split_part: String.t(),
winner_after: non_neg_integer(),
winner_after_type: String.t(),
values: [String.t()]
}
}
new_campaign_response()
View Source
new_campaign_response() :: %{
account_id: non_neg_integer(),
campaign_type: String.t(),
date: String.t(),
id: non_neg_integer(),
mail_id: non_neg_integer(),
options: %{
campaign_type: String.t(),
campaign_step: String.t(),
date: String.t(),
send_type: String.t()
}
}
new_campaign_response() :: %{
account_id: non_neg_integer(),
campaign_type: String.t(),
date: String.t(),
id: non_neg_integer(),
mail_id: non_neg_integer(),
options: %{
campaign_type: String.t(),
campaign_step: String.t(),
date: String.t(),
send_type: String.t()
}
}
Link to this section Functions
cancel(campaign)
View Source
cancel(MailerLite.id()) :: {:ok, struct()} | {:error, struct()}
cancel(MailerLite.id()) :: {:ok, struct()} | {:error, struct()}
Cancels a campaign which has outbox status.
TODO Use Elixir native Date for input/output
Example requests
MailerLite.Campaigns.cancel(6654216)
Example response
%{account_id: 123456,
campaign_name: "An email campaign",
campaign_type: "regular",
clicked: null,
count: null,
date: "2016-05-30 13:45:23",
end_time: "2016-06-30 13:45:23",
id: 1234567,
mails: [%{code: "t4h8j0",
date: "2015-12-28 17:25:31",
from: "demo@mailerlite.com",
from_name: "Demo",
groups: [%{active: 7,
bounced: 0,
clicked: 1,
date: "2015-12-16 14:43:46"
id: 2984475,
junk: 0,
name: "Personal",
opened: 2,
ordering: 5,
sent: 4,
total: 7,
updated: "2016-01-29 07:45:54"
updating: 0,
unconfirmed: 0,
unsubscribed: 0}],
host: "mailerlite.com",
id: 2851096,
language: %{code: "en",
title: "English"},
send_date: "2016-09-30 15:15:00",
subject: "Test regular campaign",
type: "custom_html",
updated: "2016-02-04 11:55:01",
url: "Test-regular-campaign-2851096"}]
mail_id: 0987543,
opened: null,
send_date: "2016-09-30 15:15:00",
status: "draft",
timezone: "120"}
Tests
iex> new_campaign = %{groups: [24992054],
iex> subject: "A temporary campaign",
iex> type: "regular"}
iex> {:ok, new_response} = MailerLite.Campaigns.new(new_campaign)
iex> campaign_id = Map.get(new_response, "id")
iex> html = ~s(<h1>Title</h1><a href="{$unsubscribe}">Unsubscribe</a>)
iex> plain = "Open HTML newsletter: {$url}. Unsubscribe: {$unsubscribe}"
iex> MailerLite.Campaigns.upload_template(campaign_id, html, plain, false)
iex> send_options = %{date: "2020-12-25 09:31",
iex> type: 2}
iex> MailerLite.Campaigns.send(campaign_id, send_options)
iex> {:ok, response} = MailerLite.Campaigns.cancel(campaign_id)
iex> MailerLite.Campaigns.delete(campaign_id)
iex> is_map(response)
true
iex> MailerLite.Campaigns.cancel(0000001)
{:error, :not_found}
iex> MailerLite.Campaigns.cancel("47")
{:error, :invalid_argument}
delete(campaign)
View Source
delete(MailerLite.id()) :: {:ok, map()} | {:error, atom()}
delete(MailerLite.id()) :: {:ok, map()} | {:error, atom()}
Delete a (non scheduled) campaign.
Example requests
MailerLite.Campaigns.delete(6345868)
Example response
{:ok, %{"success" => true}}
Test
iex> new_campaign = %{groups: [24992054],
iex> subject: "A regular email campaign",
iex> type: "regular"}
iex> {:ok, response} = MailerLite.Campaigns.new(new_campaign)
iex> campaign_id = Map.get(response, "id")
iex> MailerLite.Campaigns.delete(campaign_id)
{:ok, %{"success" => true}}
get(status) View Source
get(status, options)
View Source
get(:sent | :outbox | :draft, get_options()) ::
{:ok, [campaign()]} | {:error, atom()}
get(:sent | :outbox | :draft, get_options()) :: {:ok, [campaign()]} | {:error, atom()}
Returns all campaigns you have in your account by status.
Valid statuses:
:sent:outbox:draft
When using get/0 the :sent (default) status is used.
Sort and paginate
When using get/2 you can provide a map of options to enable sorting and pagination:
%{limit: 10,
offset: 0,
order: "DESC"} # DESC or ASC
Example requests
MailerLite.Campaigns.get
MailerLite.Campaigns.get(:draft)
MailerLite.Campaigns.get(:sent, %{order: "ASC"})
Example response
{:ok, [%{clicked: %{count: 2, rate: 40},
date_created: "2016-02-17 15:22:40",
date_send: "2016-02-17 15:28:40",
id: 2825239,
name: "Email campaign example",
opened: %{count: 1, rate: 20},
status: "sent",
total_recipients: 35,
type: "regular"}]}
Tests
iex> {:ok, response} = MailerLite.Campaigns.get
iex> is_list(response)
true
iex> {:ok, response} = MailerLite.Campaigns.get(:sent, %{order: "ASC"})
iex> is_list(response)
true
iex> {:error, :invalid_argument} = MailerLite.Campaigns.get("4092739", :options)
{:error, :invalid_argument}
iex> {:error, :invalid_status} = MailerLite.Campaigns.get(:stared)
{:error, :invalid_status}
new(new_campaign)
View Source
new(new_campaign()) :: {:ok, new_campaign_response()} | {:error, atom()}
new(new_campaign()) :: {:ok, new_campaign_response()} | {:error, atom()}
Create a new campaign. Returns the new campaign details.
Example requests
new_campaign = %{groups: [24992054, 25000854],
subject: "A regular email campaign",
type: "regular"}
MailerLite.Campaigns.new(new_campaign)
Example response
{:ok, %{account_id: 441087,
campaign_type: "regular",
date: "2016-05-18 13:03:47",
id: 3043021,
mail_id: 3529037,
options: %{campaign_type: "regular",
campaign_step: "step3",
date: "2016-05-18 13:03:47",
send_type: "regular"}}}
Tests
iex> new_campaign = %{groups: [24992054, 25000854],
iex> subject: "A regular email campaign",
iex> type: "regular"}
iex> {:ok, response} = MailerLite.Campaigns.new(new_campaign)
iex> campaign_id = Map.get(response, "id")
iex> MailerLite.Campaigns.delete(campaign_id)
iex> is_map(response)
true
iex> MailerLite.Campaigns.new(47)
{:error, :invalid_argument}
send(campaign)
View Source
send(MailerLite.id()) :: {:ok, struct()} | {:error, atom()}
send(MailerLite.id()) :: {:ok, struct()} | {:error, atom()}
send(campaign, options)
View Source
send(MailerLite.id(), struct()) :: {:ok, struct()} | {:error, atom()}
send(MailerLite.id(), struct()) :: {:ok, struct()} | {:error, atom()}
Schedule and send a campaign that has draft status and has step value equal to 3.
TODO Use Elixir native Date for input/output
Example requests
MailerLite.Campaigns.send(6654216)
send_options = %{analytics: 1,
date: "2017-05-01 09:31:00",
type: 2,
followup_date: "2017-05-01 09:31",
followup_schedule: "specific"
followup_timezone_id: 2}
MailerLite.Campaigns.send(6654216, send_options)
Example response
%{account_id: 123456,
campaign_name: "An email campaign",
campaign_type: "regular",
clicked: null,
count: null,
date: "2016-05-30 13:45:23",
end_time: "2016-06-30 13:45:23",
id: 1234567,
mails: [%{code: "t4h8j0",
date: "2015-12-28 17:25:31",
from: "demo@mailerlite.com",
from_name: "Demo",
groups: [%{active: 7,
bounced: 0,
clicked: 1,
date: "2015-12-16 14:43:46"
id: 2984475,
junk: 0,
name: "Personal",
opened: 2,
ordering: 5,
sent: 4,
total: 7,
updated: "2016-01-29 07:45:54"
updating: 0,
unconfirmed: 0,
unsubscribed: 0}],
host: "mailerlite.com",
id: 2851096,
language: %{code: "en",
title: "English"},
send_date: "2016-09-30 15:15:00",
subject: "Test regular campaign",
type: "custom_html",
updated: "2016-02-04 11:55:01",
url: "Test-regular-campaign-2851096"}]
mail_id: 0987543,
opened: null,
send_date: "2016-09-30 15:15:00",
status: "draft",
timezone: "120"}
Tests
iex> new_campaign = %{groups: [24992054],
iex> subject: "A temporary campaign",
iex> type: "regular"}
iex> {:ok, new_response} = MailerLite.Campaigns.new(new_campaign)
iex> campaign_id = Map.get(new_response, "id")
iex> html = ~s(<h1>Title</h1><a href="{$unsubscribe}">Unsubscribe</a>)
iex> plain = "Open HTML newsletter: {$url}. Unsubscribe: {$unsubscribe}"
iex> MailerLite.Campaigns.upload_template(campaign_id, html, plain, false)
iex> send_options = %{date: "2020-12-25 09:31",
iex> type: 2}
iex> {:ok, response} = MailerLite.Campaigns.send(Map.get(new_response, "id"), send_options)
iex> MailerLite.Campaigns.cancel(campaign_id)
iex> MailerLite.Campaigns.delete(campaign_id)
iex> is_map(response)
true
iex> MailerLite.Campaigns.send(0000001)
{:error, :not_found}
iex> MailerLite.Campaigns.send("campaign", 4)
{:error, :invalid_argument}
upload_template(campaign, html, plain, auto_inline)
View Source
upload_template(MailerLite.id(), String.t(), String.t(), boolean()) ::
{:ok, map()} | {:error, atom()} | {:error, non_neg_integer(), String.t()}
upload_template(MailerLite.id(), String.t(), String.t(), boolean()) :: {:ok, map()} | {:error, atom()} | {:error, non_neg_integer(), String.t()}
Uploads an HTML and plain text template to a campaign.
Important
Your HTML template must contain an unsubscribe link:
<a href="{$unsubscribe}">Unsubscribe</a>
Your plain text email should contain these variables:
{$unsubscribe}: Unsubscribe link{$url}: URL to your HTML newsletter
Example request
campaign = 3043021
html = ~s(<h1>Title</h1><a href="{$unsubscribe}">Unsubscribe</a>)
plain = "Open HTML newsletter: {$url}. Unsubscribe: {$unsubscribe}"
auto_inline = false
MailerLite.Campaigns.upload_template(campaign, html, plain, auto_inline)
Example response
{:ok}
Tests
iex> html = ~s(<h1>Title</h1><a href="{$unsubscribe}">Unsubscribe</a>)
iex> plain = "Open HTML newsletter: {$url}. Unsubscribe: {$unsubscribe}"
iex> MailerLite.Campaigns.upload_template(6654216, html, plain, false)
{:ok, %{"success" => true}}
iex> html = ~s(<h1>Title</h1>)
iex> plain = "Open HTML newsletter: {$url}."
iex> MailerLite.Campaigns.upload_template(6654216, html, plain, false)
{:error, :unprocessable_entity}
iex> MailerLite.Campaigns.upload_template("campaign", 4, :banana, 69)
{:error, :invalid_argument}