/
sparkpost.sender.ex
executable file
·83 lines (72 loc) · 2.21 KB
/
sparkpost.sender.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
defmodule Krug.SparkPostSender do
@moduledoc """
A module to simplify use of Bamboo SMTP mailing
with the "SparkPost" smtp provider.
Documentation at https://www.sparkpostmail.com
"""
@moduledoc since: "0.3.0"
alias Krug.GenericMail
alias Krug.GenericSMTPMailer
@doc """
Send mail over "SparkPost" smtp provider.
Don't uses config.exs file for configuration read. Uses following
conventioned configurations:
```
[
adapter: Bamboo.SMTPAdapter,
chained_adapter: Bamboo.SMTPAdapter,
server: "",
port: 587,
username: "",
password: "",
api_key: "my_api_key",
tls: :always,
allowed_tls_versions: {:system, "ALLOWED_TLS_VERSIONS"},
ssl: false,
retries: 0,
no_mx_lookups: false,
auth: :always
]
```
Received configurations options will be merged in this above. Configurations
specific for this smtp provider too.
Return true if call to smtp provider results without errors. However the mail can be not delivered
for any reason, by example restrictions quotas.
```elixir
iex > email_configuration = %{
sender_name: "Johannes Backend",
sender_email: "johannes@has.not.email",
sender_password: nil,
replay_to: "johannes_chief@johannesenterpriseserver.com"
}
iex > Krug.SparkPostSender.mail(email_configuration,"Lets Party","Party Tommorow at 18:30","any@any.com")
false
```
```elixir
iex > email_configuration = %{
sender_name: "Johannes Backend",
sender_email: "johannes@has.not.email",
sender_password: "<johannes_password>",
replay_to: "johannes_chief@johannesenterpriseserver.com"
}
iex > Krug.SparkPostSender.mail(email_configuration,"Lets Party","Party Tommorow at 18:30","any@any.com")
true
```
"""
def mail(email_configuration,title,body,tto) do
try do
try do
credentials = %{server: "smtp.sparkpostmail.com",
username: "SMTP_Injection",
password: email_configuration.sender_password}
GenericMail.get_email(email_configuration,title,body,tto)
|> GenericSMTPMailer.deliver_now(credentials)
true
rescue
_ -> false
end
catch
_ -> false
end
end
end