View Source Swoosh.Adapters.Sendgrid (Swoosh v1.6.0)

An adapter that sends email using the Sendgrid API.

For reference: Sendgrid API docs

Example

# config/config.exs
config :sample, Sample.Mailer,
  adapter: Swoosh.Adapters.Sendgrid,
  api_key: "my-api-key"

# lib/sample/mailer.ex
defmodule Sample.Mailer do
  use Swoosh.Mailer, otp_app: :sample
end

Using with provider options

import Swoosh.Email

new()
|> from({"Xu Shang-Chi", "xu.shangchi@example.com"})
|> to({"Katy", "katy@example.com"})
|> reply_to("xu.xialing@example.com")
|> cc("yingli@example.com")
|> cc({"Xu Wenwu", "xu.wenwu@example.com"})
|> bcc("yingnan@example.com")
|> bcc({"Jon Jon", "jonjon@example.com"})
|> subject("Hello, Ten Rings!")
|> html_body("<h1>Hello</h1>")
|> text_body("Hello")
|> put_provider_option(:custom_args, %{
  my_var: %{my_message_id: 123},
  my_other_var: %{my_other_id: 1, stuff: 2}
})
|> put_provider_option(:asm, %{
  "group_id" => 1,
  "groups_to_display" => [1, 2, 3]
})
|> put_provider_option(:categories, ["welcome"])
|> put_provider_option(:mail_settings, %{
  sandbox_mode: %{enable: true}
})
|> put_provider_option(:tracking_settings, %{
  subscription_tracking: %{enable: false}
})

Provider Options

Supported provider options are the following:

Inserted into personalization

  • :custom_args (map) - key/value pairs custom arguments that specific to this personalization

  • :substitutions (map) - key/value pairs of substitutions string applied to the :subject and :reply-to parameter

  • :dynamic_template_data (map) - key/value pairs of dynamic template data used in Dynamic Transactional Templates, see :template_id

Inserted into request body

  • :template_id (string) - an email template ID

  • :asm (map) - a map contains fields below on how to handle unsubscribes

  • :categories (list[string]) - list of category name for this message

  • :mail_settings (map) - collection of mail settings to handle this email

  • :tracking_settings (map) - collection of settings to track the metrics of responses of email recipients

Sandbox mode

For sandbox mode, use put_provider_option/3:

iex> new() |> put_provider_option(:mail_settings, %{sandbox_mode: %{enable: true}})

Link to this section Summary

Link to this section Functions

Callback implementation for Swoosh.Adapter.validate_config/1.

Callback implementation for Swoosh.Adapter.validate_dependency/0.