Skip to content

Create and send composable emails with Elixir and SendGrid.

License

Notifications You must be signed in to change notification settings

wealthfit/sendgrid_elixir

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SendGrid

A wrapper for SendGrid's API to create composable emails. Check the docs for complete usage.

Example

SendGrid.Email.build()
|> SendGrid.Email.add_to("[email protected]")
|> SendGrid.Email.put_from("[email protected]")
|> SendGrid.Email.put_subject("Hello from Elixir")
|> SendGrid.Email.put_text("Sent with Elixir")
|> SendGrid.Mailer.send()

Installation

Add the following code to your dependencies in your mix.exs file:

{:sendgrid, "~> 1.8.0"}

Configuration

In one of your configuration files, include your SendGrid API key like this:

config :sendgrid,
  api_key: "SENDGRID_API_KEY"

If you want to use environment variable, use {:system, "ENV_NAME"} in your config:

config :sendgrid,
  api_key: {:system, "SENDGRID_API_KEY"}

If you'd like to enable sandbox mode (emails won't send but will be validated), add the setting to your config:

config :sendgrid,
  api_key: "SENDGRID_API_KEY",
  sandbox_enable: true

Add :sendgrid to your list of applications if using Elixir 1.3 or lower.

defp application do
  [applications: [:sendgrid]]
end

Phoenix Views

You can use Phoenix Views to set your HTML and text content of your emails. You just have to provide a view module and template name and you're good to go! Additionally, you can set a layout to render the view in with SendGrid.Email.put_phoenix_layout/2. See SendGrid.Email.put_phoenix_template/3 for complete usage.

Examples

import SendGrid.Email

# Using an HTML template
%SendGrid.Email{}
|> put_phoenix_view(MyApp.Web.EmailView)
|> put_phoenix_template("welcome_email.html", user: user)

# Using a text template
%SendGrid.Email{}
|> put_phoenix_view(MyApp.Web.EmailView)
|> put_phoenix_template("welcome_email.txt", user: user)

# Using both an HTML and text template
%SendGrid.Email{}
|> put_phoenix_view(MyApp.Web.EmailView)
|> put_phoenix_template(:welcome_email, user: user)


# Setting the layout
%SendGrid.Email{}
|> put_phoenix_layout({MyApp.Web.EmailView, :layout})
|> put_phoenix_view(MyApp.Web.EmailView)
|> put_phoenix_template(:welcome_email, user: user)

Using a Default Phoenix View

You can set a default Phoenix View to use for rendering templates. Just set the :phoenix_view config value

config :sendgrid,
  phoenix_view: MyApp.Web.EmailView

Using a Default Layout

You can set a default layout to render your view in. Set the :phoenix_layout config value.

config :sendgrid,
  phoenix_layout: {MyApp.Web.EmailView, :layout}

About

Create and send composable emails with Elixir and SendGrid.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Elixir 99.6%
  • HTML 0.4%