All Projects → swoosh → Swoosh

swoosh / Swoosh

Licence: mit
Compose, deliver and test your emails easily in Elixir

Programming Languages

elixir
2628 projects

Labels

Projects that are alternatives of or similar to Swoosh

Truemail
🚀 Configurable framework agnostic plain Ruby 📨 email validator/verifier. Verify email via Regex, DNS and SMTP. Be sure that email address valid and exists.
Stars: ✭ 717 (-22.99%)
Mutual labels:  email
Node Dkim Key
DKIM (DomainKeys Identified Mail) Key
Stars: ✭ 5 (-99.46%)
Mutual labels:  email
Email
Faster MIME Mail Parser
Stars: ✭ 19 (-97.96%)
Mutual labels:  email
Notify
A dead simple Go library for sending notifications to various messaging services.
Stars: ✭ 727 (-21.91%)
Mutual labels:  email
Dank Selfhosted
Automated solution for hosting email, web, DNS, XMPP, and ZNC on OpenBSD.
Stars: ✭ 800 (-14.07%)
Mutual labels:  email
Sendgrid Csharp
The Official Twilio SendGrid Led, Community Driven C#, .NetStandard, .NetCore API Library
Stars: ✭ 835 (-10.31%)
Mutual labels:  email
Sendgrid Go
The Official Twilio SendGrid Led, Community Driven Golang API Library
Stars: ✭ 710 (-23.74%)
Mutual labels:  email
Beautymail
Send beautiful HTML emails with Laravel
Stars: ✭ 923 (-0.86%)
Mutual labels:  email
Mailtrackerblocker
Email tracker, read receipt and spy pixel blocker plugin for macOS Apple Mail
Stars: ✭ 821 (-11.82%)
Mutual labels:  email
React email editor
This project is experimental! It's my attempt to create visual email template editor using React+Redux+etc... tools stack.
Stars: ✭ 19 (-97.96%)
Mutual labels:  email
Python O365
A simple python library to interact with Microsoft Graph and Office 365 API
Stars: ✭ 742 (-20.3%)
Mutual labels:  email
Bootstrap Email
Bootstrap 4 (and soon 5) stylesheet, compiler, and inliner for responsive and consistent emails with the Bootstrap syntax you know and love.
Stars: ✭ 781 (-16.11%)
Mutual labels:  email
Clj Mandrill
A Clojure implementation of the Mandrill API
Stars: ✭ 16 (-98.28%)
Mutual labels:  email
Lettre
a mailer library for Rust
Stars: ✭ 713 (-23.42%)
Mutual labels:  email
Notmail bot
Telegram bot that acts as an email client
Stars: ✭ 23 (-97.53%)
Mutual labels:  email
Memacs
What did I do on February 14th 2007? Visualize your (digital) life in Org-mode
Stars: ✭ 711 (-23.63%)
Mutual labels:  email
Kanmail
📥 An email client that functions like a kanban board.
Stars: ✭ 833 (-10.53%)
Mutual labels:  email
Mysigmail
UI Email Signature Generator - Make easy to create your email signature
Stars: ✭ 925 (-0.64%)
Mutual labels:  email
Django Email Confirm La
Django email confirmation for any model and any field
Stars: ✭ 23 (-97.53%)
Mutual labels:  email
Postmark Java
Official Java client library for the Postmark HTTP API
Stars: ✭ 18 (-98.07%)
Mutual labels:  email

Swoosh

hex.pm hex.pm hex.pm github.com

Compose, deliver and test your emails easily in Elixir.

We have applied the lessons learned from projects like Plug, Ecto and Phoenix in designing clean and composable APIs, with clear separation of concerns between modules. Out of the box it comes with 12 adapters, including SendGrid, Mandrill, Mailgun, Postmark, SMTP... see Adapters below

The complete documentation for Swoosh is located here.

Requirements

Elixir 1.8+ and Erlang OTP 22+

Getting started

# In your config/config.exs file
config :sample, Sample.Mailer,
  adapter: Swoosh.Adapters.Sendgrid,
  api_key: "SG.x.x"

# In your application code
defmodule Sample.Mailer do
  use Swoosh.Mailer, otp_app: :sample
end

defmodule Sample.UserEmail do
  import Swoosh.Email

  def welcome(user) do
    new()
    |> to({user.name, user.email})
    |> from({"Dr B Banner", "[email protected]"})
    |> subject("Hello, Avengers!")
    |> html_body("<h1>Hello #{user.name}</h1>")
    |> text_body("Hello #{user.name}\n")
  end
end

# In an IEx session
Sample.UserEmail.welcome(%{name: "Tony Stark", email: "[email protected]"})
|> Sample.Mailer.deliver

# Or in a Phoenix controller
defmodule Sample.UserController do
  use Phoenix.Controller
  alias Sample.UserEmail
  alias Sample.Mailer

  def create(conn, params) do
    user = # create user logic
    UserEmail.welcome(user) |> Mailer.deliver
  end
end

See Mailer docs for more configuration options.

Installation

  • Add swoosh to your list of dependencies in mix.exs:

    def deps do
      [{:swoosh, "~> 1.0"}]
    end
    
  • (Optional-ish) Most Adapters (Non SMTP ones) use Swoosh.ApiClient to talk to the service provider. Swoosh comes with Swoosh.ApiClient.Hackney. if you want to use the default, include :hackney as a dependency as well. Otherwise, define a new API client that uses the HTTP client you like, and config swoosh to use the new API Client. See Swoosh.ApiClient and Swoosh.ApiClient.Hackney for details.

    config :swoosh, :api_client, MyApp.ApiClient
    
  • (Optional) If you are using Swoosh.Adapters.SMTP, Swoosh.Adapters.Sendmail or Swoosh.Adapters.AmazonSES, you also need to add gen_smtp to your deps and list of applications:

    def deps do
      [
        {:swoosh, "~> 1.0"},
        {:gen_smtp, "~> 0.13"}
      ]
    end
    

Adapters

Swoosh supports the most popular transactional email providers out of the box and also has a SMTP adapter. Below is the list of the adapters currently included:

Provider Swoosh adapter
SMTP Swoosh.Adapters.SMTP
SendGrid Swoosh.Adapters.Sendgrid
Sendinblue Swoosh.Adapters.Sendinblue
Sendmail Swoosh.Adapters.Sendmail
Mandrill Swoosh.Adapters.Mandrill
Mailgun Swoosh.Adapters.Mailgun
Mailjet Swoosh.Adapters.Mailjet
Postmark Swoosh.Adapters.Postmark
SparkPost Swoosh.Adapters.SparkPost
Amazon SES Swoosh.Adapters.AmazonSES
Dyn Swoosh.Adapters.Dyn
SocketLabs Swoosh.Adapters.SocketLabs
Gmail Swoosh.Adapters.Gmail

Configure which adapter you want to use by updating your config/config.exs file:

config :sample, Sample.Mailer,
  adapter: Swoosh.Adapters.SMTP
  # adapter config (api keys, etc.)

Check the documentation of the adapter you want to use for more specific configurations and instructions.

Adding new adapters is super easy and we are definitely looking for contributions on that front. Get in touch if you want to help!

Recipient

The Recipient Protocol enables you to easily make your structs compatible with Swoosh functions.

defmodule MyUser do
  @derive {Swoosh.Email.Recipient, name: :name, address: :email}
  defstruct [:name, :email, :other_props]
end

Now you can directly pass %MyUser{} to from, to, cc, bcc, etc. See Swoosh.Email.Recipient for more details.

Async Emails

Swoosh does not make any special arrangements for sending emails in a non-blocking manner.

To send asynchronous emails in Swoosh, one can simply leverage Elixir's standard library:

Task.start(fn ->
  %{name: "Tony Stark", email: "[email protected]"}
  |> Sample.UserEmail.welcome
  |> Sample.Mailer.deliver
end)

Please take a look at the official docs for Task and Task.Supervisor for further options.

Note: it is not to say that Task.start is enough to cover the whole async aspect of sending emails. It is more to say that the implementation of sending emails is very application specific. For example, the simple example above might be sufficient for some small applications but not so much for more mission critical applications. Runtime errors, network errors and errors from the service provider all need to be considered and handled, maybe differently as well. Whether to retry, how many times you want to retry, what to do when everything fails, these questions all have different answers in different context.

Phoenix integration

If you are looking to use Swoosh in your Phoenix project, make sure to check out the phoenix_swoosh project. It contains a set of functions that make it easy to render the text and HTML bodies using Phoenix views, templates and layouts.

Taking the example from above the "Getting Started" section, your code would look something like this:

# web/templates/layout/email.html.eex
<html>
  <head>
    <title><%= @email.subject %></title>
  </head>
  <body>
    <%= @inner_content %>
  </body>
</html>

# web/templates/email/welcome.html.eex
<div>
  <h1>Welcome to Sample, <%= @username %>!</h1>
</div>

# web/emails/user_email.ex
defmodule Sample.UserEmail do
  use Phoenix.Swoosh, view: Sample.EmailView, layout: {Sample.LayoutView, :email}

  def welcome(user) do
    new()
    |> to({user.name, user.email})
    |> from({"Dr B Banner", "[email protected]"})
    |> subject("Hello, Avengers!")
    |> render_body("welcome.html", %{username: user.username})
  end
end

Feels familiar doesn't it? Head to the phoenix_swoosh repo for more details.

Attachments

You can attach files to your email using the Swoosh.Email.attachment/2 function. Just give the path of your file as an argument and we will do the rest. It also works with a %Plug.Upload{} struct, or a %Swoosh.Attachment{} struct, which can be constructed using Swoosh.Attachment.new detailed here in the docs.

All built-in adapters have support for attachments.

new()
|> to("[email protected]")
|> from({"Jarvis", "[email protected]"})
|> subject("Invoice May")
|> text_body("Here is the invoice for your superhero services in May.")
|> attachment("/Users/jarvis/invoice-peter-may.pdf")

Testing

In your config/test.exs file set your mailer's adapter to Swoosh.Adapters.Test so that you can use the assertions provided by Swoosh in Swoosh.TestAssertions module.

defmodule Sample.UserTest do
  use ExUnit.Case, async: true

  import Swoosh.TestAssertions

  test "send email on user signup" do
    # Assuming `create_user` creates a new user then sends out a `Sample.UserEmail.welcome` email
    user = create_user(%{username: "ironman", email: "[email protected]"})
    assert_email_sent Sample.UserEmail.welcome(user)
  end
end

Mailbox preview in the browser

Swoosh ships with a Plug that allows you to preview the emails in the local (in-memory) mailbox. It's particularly convenient in development when you want to check what your email will look like while testing the various flows of your application.

For email to reach this mailbox you will need to set your Mailer adapter to Swoosh.Adapters.Local:

# in config/dev.exs
config :sample, Mailer,
  adapter: Swoosh.Adapters.Local

# to run the preview server together as part of your app
config :swoosh, serve_mailbox: true

# to change the preview server port (4000 by default)
config :swoosh, serve_mailbox: true, preview_port: 4001

If you don't want to run the preview server as part of your app as shown above, in your Phoenix project you can also forward directly to the plug if you so choose, like this:

# in web/router.ex
if Mix.env == :dev do
  scope "/dev" do
    pipe_through [:browser]

    forward "/mailbox", Plug.Swoosh.MailboxPreview
  end
end

And finally you can also use the following Mix task to start the mailbox preview server independently though note that it won't display/process emails being sent from outside its own process (great for testing within iex).

$ mix swoosh.mailbox.server

If you are curious, this is how it looks:

Plug.Swoosh.MailboxPreview

The preview is also available as a JSON endpoint.

$ curl http://localhost:4000/dev/mailbox/json

Production

Swoosh starts a memory storage process for local adapter by default. Normally it does no harm being left around in production. However, if it is causing problems, or you don't like having it around, it can be disabled like so:

# config/prod.exs
config :swoosh, local: false

Documentation

Documentation is written into the library, you will find it in the source code, accessible from iex and of course, it all gets published to hexdocs.

Contributing

We are grateful for any contributions. Before you submit an issue or a pull request, remember to:

  • Look at our Contributing guidelines
  • Not use the issue tracker for help or support requests (try StackOverflow, IRC or Slack instead)
  • Do a quick search in the issue tracker to make sure the issues hasn't been reported yet.
  • Look and follow the Code of Conduct. Be nice and have fun!

Running tests

Clone the repo and fetch its dependencies:

$ git clone https://github.com/swoosh/swoosh.git
$ cd swoosh
$ mix deps.get
$ mix test

Building docs

$ MIX_ENV=docs mix docs

LICENSE

See LICENSE

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].