sendgrid v2.0.0 SendGrid.Email View Source
Email primitive for composing emails with SendGrid’s API.
You can easily compose on an Email to set the fields of your email.
Example
Email.build()
|> Email.add_to("test@email.com")
|> Email.put_from("test2@email.com")
|> Email.put_subject("Hello from Elixir")
|> Email.put_text("Sent with Elixir")
|> SendGrid.Mail.send()
SendGrid Specific Features
Many common features of SendGrid V3 API for transactional emails are supported.
Templates
You can use a SendGrid template by providing a template id.
put_template(email, "some_template_id")
Substitutions
You can provided a key-value pair for subsititions to have text replaced.
add_substitution(email, "-key-", "value")
Scheduled Sending
You can provide a Unix timestamp to have an email delivered in the future.
send_at(email, 1409348513)
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 put_phoenix_layout/2
. See put_phoenix_template/3
for complete usage.
Examples
# Using an HTML template
%Email{}
|> put_phoenix_view(MyApp.Web.EmailView)
|> put_phoenix_template("welcome_email.html", user: user)
# Using a text template
%Email{}
|> put_phoenix_view(MyApp.Web.EmailView)
|> put_phoenix_template("welcome_email.txt", user: user)
# Using both an HTML and text template
%Email{}
|> put_phoenix_view(MyApp.Web.EmailView)
|> put_phoenix_template(:welcome_email, user: user)
# Setting the layout
%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 View Layout
You can set a default layout to render the view in. Just set the :phoenix_layout
config value.
config :sendgrid,
phoenix_layout: {MyApp.Web.EmailView, :layout}
Link to this section Summary
Functions
Adds an attachment to the email
Add recipients to the BCC
address field. The bcc-name can be specified as the third parameter
Add recipients to the CC
address field. The cc-name can be specified as the third parameter
Adds a custom_arg value to the email
Adds a custom_arg value to the email
Sets a custom header
Adds a t:Personalization.t/0
to an email
Adds a substitution value to be used with a template
Sets the to
field for the email. A to-name can be passed as the third parameter
Builds an an empty email to compose on
Sets the from
field for the email. The from-name can be specified as the third parameter
Sets the html
content of the email
Sets the layout to use for the Phoenix Template
Renders the Phoenix template with the given assigns
Sets the Phoenix View to use
Sets the reply_to
field for the email. The reply-to name can be specified as the third parameter
Sets a future date of when to send the email
Sets the subject
field for the email
Uses a predefined SendGrid template for the email
Sets text
content of the email
Sets the email to be sent with sandbox mode enabled or disabled
Transforms an t:Email.t/0
to a t:Personalization.t/0
Link to this section Types
t() :: %SendGrid.Email{ __phoenix_layout__: nil | %{optional(:text) => String.t(), optional(:html) => String.t()}, __phoenix_view__: nil | atom(), attachments: nil | [attachment()], bcc: nil | [recipient()], cc: nil | [recipient()], content: nil | [content()], custom_args: nil | custom_args(), dynamic_template_data: nil | dynamic_template_data(), from: nil | recipient(), headers: nil | headers(), personalizations: nil | [SendGrid.Personalization.t()], reply_to: nil | recipient(), sandbox: boolean(), send_at: nil | integer(), subject: nil | String.t(), substitutions: nil | substitutions(), template_id: nil | String.t(), to: nil | [recipient()] }
Link to this section Functions
add_attachment(t(), attachment()) :: t()
Adds an attachment to the email.
An attachment is a map with the keys:
:content
:type
:filename
:disposition
:content_id
Examples
attachment = %{content: "base64string", filename: "image.jpg"}
add_attachment(%Email{}, attachment}
Add recipients to the BCC
address field. The bcc-name can be specified as the third parameter.
Examples
add_bcc(%Email{}, "test@email.com")
add_bcc(%Email{}, "test@email.com", "John Doe")
Add recipients to the CC
address field. The cc-name can be specified as the third parameter.
Examples
add_cc(%Email{}, "test@email.com")
add_cc(%Email{}, "test@email.com", "John Doe")
add_cc(SendGrid.Email.t(), String.t(), String.t()) :: SendGrid.Email.t()
Adds a custom_arg value to the email.
If an argument for a given name is already set, it will be replaced when adding a argument with the same name.
Examples
Email.add_custom_arg(%Email{}, "-sentIn-", "Elixir")
Adds a custom_arg value to the email.
If an argument for a given name is already set, it will be replaced when adding a argument with the same name.
Examples
Email.add_dynamic_template_data(%Email{}, "-sentIn-", "Elixir")
Sets a custom header.
Examples
Email.add_header(%Email{}, "HEADER_KEY", "HEADER_VALUE")
add_personalization(t(), SendGrid.Personalization.t()) :: t()
Adds a t:Personalization.t/0
to an email.
Adds a substitution value to be used with a template.
If a substitution for a given name is already set, it will be replaced when adding a substitution with the same name.
Examples
Email.add_substitution(%Email{}, "-sentIn-", "Elixir")
Sets the to
field for the email. A to-name can be passed as the third parameter.
Examples
add_to(%Email{}, "test@email.com")
add_to(%Email{}, "test@email.com", "John Doe")
Sets the from
field for the email. The from-name can be specified as the third parameter.
Examples
put_from(%Email{}, "test@email.com")
put_from(%Email{}, "test@email.com", "John Doe")
Sets the html
content of the email.
Examples
Email.put_html(%Email{}, "<html><body><p>Sent from Elixir!</p></body></html>")
Sets the layout to use for the Phoenix Template.
Expects a tuple of the view module and layout to use. If you provide an atom as the second element, the text and HMTL versions of that template will be used for the respective content types.
Alernatively, you can set a default layout to use by setting the :phoenix_view
key in your config as
an atom which will be used for both text and HTML emails.
config :sendgrid,
phoenix_layout: {MyApp.Web.EmailView, :layout}
Examples
put_phoenix_layout(email, {MyApp.Web.EmailView, "layout.html"})
put_phoenix_layout(email, {MyApp.Web.EmailView, "layout.txt"})
put_phoenix_layout(email, {MyApp.Web.EmailView, :layout})
Renders the Phoenix template with the given assigns.
You can set the default Phoenix View to use for your templates by setting the :phoenix_view
config value.
Additionally, you can set the view on a per email basis by calling put_phoenix_view/2
. Furthermore, you can have
the template rendered inside a layout. See put_phoenix_layout/2
for more details.
Explicit Template Extensions
You can provide a template name with an explicit extension such as "some_template.html"
or
"some_template.txt"
. This is set the content of the email respective to the content type of
the template rendered. For example, if you render an HTML template, the output of the rendering
will be the HTML content of the email.
Implicit Template Extensions
You can omit a template’s extension and attempt to have both a text template and HTML template
rendered. To have both types rendered, both templates must share the same base file name. For
example, if you have a template named "some_template.txt"
and a template named "some_template.html"
and you call put_phoenix_template(email, :some_template)
, both templates will be used and will
set the email content for both content types. The only caveat is both files must exist, otherwise you’ll
have an exception raised.
Examples
iex> put_phoenix_template(email, "some_template.html")
%Email{content: [%{type: "text/html", value: ...}], ...}
iex> put_phoenix_template(email, "some_template.txt", name: "John Doe")
%Email{content: [%{type: "text/plain", value: ...}], ...}
iex> put_phoenix_template(email, :some_template, user: user)
%Email{content: [%{type: "text/plain", value: ...}, %{type: "text/html", value: ...}], ...}
Sets the Phoenix View to use.
This will override the default Phoenix View if set in under the :phoenix_view
config value.
Examples
put_phoenix_view(email, MyApp.Web.EmailView)
Sets the reply_to
field for the email. The reply-to name can be specified as the third parameter.
Examples
put_reply_to(%Email{}, "test@email.com")
put_reply_to(%Email{}, "test@email.com", "John Doe")
Uses a predefined SendGrid template for the email.
Examples
Email.put_template(%Email{}, "the_template_id")
Sets the email to be sent with sandbox mode enabled or disabled.
The sandbox mode will default to what is explicity configured with SendGrid’s configuration.
to_personalization(t()) :: SendGrid.Personalization.t()
Transforms an t:Email.t/0
to a t:Personalization.t/0
.