GithubHelp home page GithubHelp logo

mailgun's Introduction

Elixir Mailgun Client Build Status

# config/config.exs

config :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!"
  end

  def send_welcome_html_email(user) do
    send_email to: user.email,
               from: @from,
               subject: "hello!",
               html: "<strong>Welcome!</strong>"
  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: "<strong>Cheers!</strong>",
               attachments: [%{path: file_path, filename: "greetings.png"}]
  end

  def send_invoice(user) do
    pdf = Invoice.create_for(user) # a string
    send_email to: user.email,
               from: @from,
               subject: "Invoice",
               html: "<strong>Your Invoice</strong>",
               attachments: [%{content: pdf, filename: "invoice.pdf"}]
  end
end


iex> MyApp.Mailer.send_welcome_text_email(user)
{:ok, ...}

Installation

Add mailgun to your mix.exs dependencies:

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.

# 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 to call Mailgun REST API. You can inject any valid httpc options to your outbound requests by defining them within httpc_opts config entry:

# 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
...

mailgun's People

Contributors

aerosol avatar bitwalker avatar chrismccord avatar dania02525 avatar davidmiller avatar davidstump avatar gazler avatar josevalim avatar joshcrews avatar manukall avatar mmcc avatar osxi avatar pma avatar victorsolis avatar vysakh0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

mailgun's Issues

How to send attachments?

Hi :)
It would be nice to have an example (or test case to look at) on how to send an email with some files attached.

System.get_env

I may be totally looking at this wrong and would love to be corrected.

It looks like if you did the config like this:

config :my_app, mailgun_domain: "https://api.mailgun.net/v3/mydomain.com",
                mailgun_key: System.get_env("MAILGUN_API_KEY")

It's going to pull the environment variable at compile time since the config happens in a macro? The problem, I'm running into is that at runtime the env variable may be different than the compile time one (if there's even a compile time one there).

Attachment + unicode char in subject/body = hangs with no response

When we have attachment and ascii symbols in subject/body - everything is fine.

When we have something like this:

send_email(
        to: "[email protected]", 
        from: "[email protected]",
        subject: "Тестовое письмо",
        html: "Test body",
        attachments: [%{path: "mix.exs", filename: "mix.exs"}]
)

we get mailgun hangs with no response at all.

I added :httpc.set_options([{:verbose, :debug}]) to the client.ex right before calling :httpc.request and attached debug output for you.
Hope @chrismccord will find some time to fix it.
Thank you!

elixir 1.3.4

httpc_debug.txt

Error in send_without_attachments/2

Simple mailer, shown below gets the following error when called. Works fine is test mode but trying to send an actual email fails. This is with Elixir 1.1.0 and Erlang 18.1.

** (FunctionClauseError) no function clause matching in IO.chardata_to_string/1
    (elixir) lib/io.ex:333: IO.chardata_to_string(nil)
    (elixir) lib/path.ex:467: Path.join/2
    (elixir) lib/path.ex:449: Path.join/1
             lib/client.ex:44: Mailgun.Client.send_without_attachments/2
defmodule BrochureSite.Mailer do
  use Mailgun.Client, domain: Application.get_env(:my_app, :mailgun_domain),
                      key: Application.get_env(:my_app, :mailgun_key)

  @from "[email protected]"
  @to_sales "[email protected]"

  def send_contact_me_email do
    send_email to: @to_sales,
               from: @from,
               subject: "Sales Lead",
               text: "There's a customer that wants to know more."
  end

end

I'm on heroku so config looks like:

config :brochure_site, mailgun_domain: System.get_env("MAILGUN_DOMAIN"),
        mailgun_key: System.get_env("MAILGUN_API_KEY")

0.1.3 is not available via mix deps.get

○ mix deps.get
** (Mix) No package version in registry matches mailgun ~> 0.1.3 (from: mix.exs)

This version is required to resolve a failed dependency issue.

Sending with an attachment breaks encoding

I was trying to debug why my e-mails where getting diamond-question-marks (�, a.k.a. unicode replacement character) for marked characters (like ç or ã) when sent from my project, but not when i used curl. I noticed that only Mailgun.Client.send_without_attachments executed any form of encoding, more specifically this: URI.encode_query(Dict.drop(attrs, [:attachments])). Which makes sense because encoding for multipart form fields seems to work differently (see section 4.5). Commenting out the attachment made the encoding fine again. I'm sorry if I'm way wrong about the solution, I'm new to elixir, but I'll fork and try to add the charset and see what happens.

:error, 404

For a simple module:

defmodule Ticketing.Mailer do
    @config     domain: Application.get_env(:ticketing, :mailgun_domain),
                key: Application.get_env(:ticketing, :mailgun_key)
                #mode: :test,
                #test_file_path: "/tmp/mailgun.json"

    use Mailgun.Client, @config


    @from "[email protected]"

    def send_welcome_text_email(to) do
        send_email to: to,
               from: @from,
               subject: "hello!",
               text: "Welcome!"
    end


end

And response is:

{:error, 404,
 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n<title>404 Not Found</title>\n<h1>Not Found</h1>\n<p>The requested URL was not found on the server.</p><p>If you entered the URL manually please check your spelling and try again.</p>\n"}

Domain and key are correct and works on cURL,. Any ideas?

poison dependency conflicts with other package

Hello I created an api for a webservice but when I try to import it in my phoenix application I have the following error message:

Conflict on poison 2.0.1
  /home/me/Projects/elixir/lemonwex/mix.exs: 2.0.1
  mailgun 0.1.2: ~> 1.4

** (Mix) Hex dependency resolution failed, relax the version requirements or unlock dependencies

The project lemonwex uses poison 2.0.1 internally. I don't understand why it conflicts with the one mailgun uses. How can I fix this? I quick fixed this by retrograding the version to 1.5.2 so that mailgun doesn't conflict but there is certainly a better way

Bump version on hex.pm to 0.0.3

Is it possible yet to bump this to the next version on hex.pm? The test file changes (if they are ready) would be good to have in a version. Thx

Mailgun seems abandoned.

@chrismccord are you looking for a new owner? We are glad you put this together, but it would appear the issues and pull requests have been piling up for the last year and the new release on master hasn't been released yet.

We have forked and will continue our own development, but it would be nice to keep in the loop of any future improvements you and the contributors have planned.

Thanks!

Default port unset

When I try to send a mail in dev-mode I get following error when I turn off test-mode:

FunctionClauseError) no function clause matching in :httpc.default_port/1 (inets) httpc.erl:334: :httpc.default_port(:"") (inets) httpc.erl:542: :httpc.handle_request/9
My config:

Configures the endpoint

config :fajalobi, FajalobiWeb.Endpoint,
http: [port: 4000],
url: [host: "localhost", port: 4000],

My function:

def sendMail(params) do

send_email(%{to: Application.get_env(:fajalobi, :infomail),
from: Map.get(params, "email"),
subject: "Nieuw bericht van #",
html: Phoenix.View.render_to_string(FajalobiWeb.MailView, "info.html", %{name: "#{Map.get(params, "firstname")} #{Map.get(params, "lastname")}", email: Map.get(params, "email"), message: Map.get(params, "message") })
})
end

401 response when defining local ip in endpoint config

I’m having trouble getting mailgun to play nice in production. When defining the local IP in the config as follows:

http: [ip: {127, 0, 0, 1}, port: {:system, "PORT"}],
url: [host: "redacteddomain.com", port: 443],

This causes mailgun to return a 401 error. Simply removing ip: {127, 0, 0, 1} resolves the issue.

I’ve got that in place to prevent direct outside access to the ip:port (as otherwise you can can access the app directly on an http connection which is what the phoenix app is using, SSL is handled by nginx).

Is this expected behaviour or issue-worthy?

Config errors when a dependency depends on Mailgun

Hi, I'm having some issues with the config being set when using mailgun from within a library I'm building, it's very possible I'm at fault here but I'm new to Elixir so maybe you can point me in the right direction...

I'm getting this error:

[error] #PID<0.454.0> running HrTest.Endpoint terminated
Server: localhost:4000 (http)
Request: POST /user/new
** (exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in IO.chardata_to_string/1
        (elixir) lib/io.ex:330: IO.chardata_to_string(nil)
        (elixir) lib/path.ex:467: Path.join/2
        (elixir) lib/path.ex:449: Path.join/1
        (mailgun) lib/client.ex:44: Mailgun.Client.send_without_attachments/2
        (hr) lib/hr/form_controller.ex:258: Hr.FormController.create_signup/2
        (hr) lib/hr/form_controller.ex:257: Hr.FormController.action/2
        (hr) lib/hr/form_controller.ex:257: Hr.FormController.phoenix_controller_pipeline/2
        (hr_test) lib/phoenix/router.ex:255: HrTest.Router.dispatch/2
        (hr_test) web/router.ex:1: HrTest.Router.do_call/2
        (hr_test) lib/hr_test/endpoint.ex:1: HrTest.Endpoint.phoenix_pipeline/1
        (hr_test) lib/plug/debugger.ex:90: HrTest.Endpoint."call (overridable 3)"/2
        (hr_test) lib/phoenix/endpoint/render_errors.ex:34: HrTest.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

So, I've got a dependency to my main application which has a mailer like this:

defmodule Hr.MailHelper do
  use Mailgun.Client, domain: Application.get_env(:hr, :mailgun_domain),
                     key: Application.get_env(:hr, :mailgun_key),
                     mode: Application.get_env(:hr, :mailgun_mode),
                     test_file_path: Application.get_env(:hr, :mailgun_test_file_path)
  @dir "web/templates/hr_email/"

  def send_confirmation_email(user, link) do
    send_email to: user.unconfirmed_email,
               from: Application.get_env(:hr, :register_from_email),
               subject: Application.get_env(:hr, :confirmation_email_subject),
               html: EEx.eval_file(@dir <> "signup_confirmation.eex", [user: user, link: link])
  end

  def send_reset_email(user, link) do
    send_email to: user.email,
               from: Application.get_env(:hr, :password_recovery_from_email),
               subject: Application.get_env(:hr, :recovery_email_subject),
               html: EEx.eval_file(@dir <> "password_reset.eex", [user: user, link: link])
  end
end

and the main application (which depends on the library which includes that mailer) has these configs set as:

config :hr, register_from_email: "Registration <[email protected]>",
            password_recovery_from_email: "Password Recovery <[email protected]>",
            confirmation_email_subject: "Confirm Your Account",
            recovery_email_subject: "Reset Your Password",
            mailgun_domain: "https://api.mailgun.net/v3/mydomain.com",
            mailgun_key: "key-##############",
            mailgun_mode: :test,
            mailgun_test_file_path: "/tmp/mail.json"

I've done a bit of digging and if I pop a IO.inspect conf and IO.inspect email in at Line 44 of mailgun/lib/client.ex, it is getting the email, but not the conf, so my application config is working... The other interesting thing is that if I make a change to the mailer in the library, it recompiles(?) and then everything works. If I include the library into a fresh mix.phoenix project, it breaks again (in the new application, but not the old one). I am guessing this is something to do with compilation of macros?

FWIW I've tried using {:hr, path: "../path"} and {:hr, git: "some-git-url"} in the hope that if it's fetched remotely it will be recompiled and achieve the same effect as making a change to the source, but they behave exactly the same.

Do you have any idea what could be causing this / Is there a way around it?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.