Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chrismccord/mailgun
Elixir Mailgun Client
https://github.com/chrismccord/mailgun
Last synced: 2 days ago
JSON representation
Elixir Mailgun Client
- Host: GitHub
- URL: https://github.com/chrismccord/mailgun
- Owner: chrismccord
- License: mit
- Created: 2014-10-01T03:20:45.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2022-10-22T00:53:27.000Z (about 2 years ago)
- Last Synced: 2024-10-13T09:52:51.522Z (2 months ago)
- Language: Elixir
- Size: 50.8 KB
- Stars: 194
- Watchers: 7
- Forks: 95
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- freaking_awesome_elixir - Elixir - Elixir Mailgun Client. (Third Party APIs)
- fucking-awesome-elixir - mailgun - Elixir Mailgun Client. (Third Party APIs)
- awesome-elixir - mailgun - Elixir Mailgun Client. (Third Party APIs)
README
# Elixir Mailgun Client [![Build Status](https://travis-ci.org/chrismccord/mailgun.svg)](https://travis-ci.org/chrismccord/mailgun)
```elixir
# config/config.exsconfig :my_app, mailgun_domain: "https://api.mailgun.net/v3/mydomain.com",
mailgun_key: "key-##############"# lib/mailer.ex
defmodule MyApp.Mailer do
@config domain: Application.get_env(:my_app, :mailgun_domain),
key: Application.get_env(:my_app, :mailgun_key)
use Mailgun.Client, @config
@from "[email protected]"
def send_welcome_text_email(user) do
send_email to: user.email,
from: @from,
subject: "hello!",
text: "Welcome!"
enddef send_welcome_html_email(user) do
send_email to: user.email,
from: @from,
subject: "hello!",
html: "Welcome!"
end# attachments expect a list of maps. Each map should have a filename and path/content
def send_greetings(user, file_path) do
send_email to: user.email,
from: @from,
subject: "Happy b'day",
html: "Cheers!",
attachments: [%{path: file_path, filename: "greetings.png"}]
enddef send_invoice(user) do
pdf = Invoice.create_for(user) # a string
send_email to: user.email,
from: @from,
subject: "Invoice",
html: "Your Invoice",
attachments: [%{content: pdf, filename: "invoice.pdf"}]
end
endiex> MyApp.Mailer.send_welcome_text_email(user)
{:ok, ...}
```### Installation
Add mailgun to your `mix.exs` dependencies:
```elixir
def deps do
[ {:mailgun, "~> 0.1.2"} ]
end
```### Test mode
For testing purposes mailgun can output emails to a local file instead of
actually sending them. Just set the `mode` configuration key to `:test`
and the `test_file_path` to where you want that file to appear.```elixir
# lib/mailer.ex
defmodule MyApp.Mailer do
@config domain: Application.get_env(:my_app, :mailgun_domain),
key: Application.get_env(:my_app, :mailgun_key),
mode: :test,
test_file_path: "/tmp/mailgun.json"
use Mailgun.Client, @config...
end
```### httpc options
Under the hood the client uses [`httpc`](http://erlang.org/doc/man/httpc.html)
to call Mailgun REST API. You can inject any valid `httpc` options to your
outbound requests by defining them within `httpc_opts` config entry:```elixir
# lib/mailer.ex
defmodule MyApp.Mailer do
@config domain: Application.get_env(:my_app, :mailgun_domain),
key: Application.get_env(:my_app, :mailgun_key),
httpc_opts: [connect_timeout: 2000, timeout: 3000]
use Mailgun.Client, @config
...
```