Bamboo.SendGridHelper (bamboo v2.2.0) View Source

Functions for using features specific to Sendgrid.

Example

email
|> with_template("80509523-83de-42b6-a2bf-54b7513bd2aa")
|> substitute("%name%", "Jon Snow")
|> substitute("%location%", "Westeros")

Link to this section Summary

Functions

Add a property to the list of dynamic template data in the SendGrid template. This will be added to the request as

Add SendGrid personalizations

Add a tag to the list of substitutions in the SendGrid template.

An integer id for an ASM (Advanced Suppression Manager) group that this email should belong to. This can be used to let recipients unsubscribe from only a certain type of communication.

A boolean setting to instruct SendGrid to bypass list management for this email. If enabled, SendGrid will ignore any email supression (such as unsubscriptions, bounces, spam filters) for this email. This is useful for emails that all users must receive, such as Terms of Service updates, or password resets.

An array of category names for this email. A maximum of 10 categories can be assigned to an email. Duplicate categories will be ignored and only unique entries will be sent.

Instruct SendGrid to enable or disable Google Analytics tracking, and optionally set the UTM parameters for it. This is useful if you need to control UTM tracking parameters on an individual email basis.

Specify the ip pool name.

Schedule a time for SendGrid to deliver the email.

Specify the template for SendGrid to use for the context of the substitution tags.

A map of unique arguments for this email. This will override any existing unique arguments.

Link to this section Functions

Link to this function

add_dynamic_field(email, field, value)

View Source

Add a property to the list of dynamic template data in the SendGrid template. This will be added to the request as:

 "personalizations":[
    {
       "to":[
          {
             "email":"example@sendgrid.net"
          }
       ],
       "dynamic_template_data":{
          "total":"$ 239.85",
       }
    }
 ],

The tag can be of any type since SendGrid allows you to use Handlebars in its templates

Example

email
|> add_dynamic_field("name", "Jon Snow")
Link to this function

add_personalizations(email, personalizations)

View Source

Specs

add_personalizations(Bamboo.Email.t(), [map()]) :: Bamboo.Email.t()

Add SendGrid personalizations

Each personalization can have the following fields: to, cc, bcc, subject, headers, substitutions, custom_args, or send_at.

Settings from the top level of the email (e.g., Email |> with_send_at) will not be applied to each personalization. If you want multiple personalizations with common properties, it is recommended to generate the list from a common base value and simply do not set the corresponding top-level fields.

Example:

base_personalization = %{

bcc: [%{"email" => "bcc@bar.com", "name" => "BCC"}],
subject: "Here is your email"

}

personalizations =

Enum.map(
  [
    %{to: "one@test.com"},
    %{to: "two@test.com", send_at: 1_580_485_560}
  ],
  &Map.merge(base_personalization, &1)
)

email =

new_email()
|> Email.put_header("Reply-To", "reply@foo.com")
|> Bamboo.SendGridHelper.add_personalizations(personalizations)
Link to this function

substitute(email, tag, value)

View Source

Add a tag to the list of substitutions in the SendGrid template.

The tag must be a String.t due to SendGrid using special characters to wrap tags in the template.

Example

email
|> substitute("%name%", "Jon Snow")
Link to this function

with_asm_group_id(email, asm_group_id)

View Source

An integer id for an ASM (Advanced Suppression Manager) group that this email should belong to. This can be used to let recipients unsubscribe from only a certain type of communication.

Example

email
|> with_asm_group_id(1234)
Link to this function

with_bypass_list_management(email, enabled)

View Source

A boolean setting to instruct SendGrid to bypass list management for this email. If enabled, SendGrid will ignore any email supression (such as unsubscriptions, bounces, spam filters) for this email. This is useful for emails that all users must receive, such as Terms of Service updates, or password resets.

Example

email
|> with_bypass_list_management(true)
Link to this function

with_categories(email, categories)

View Source

An array of category names for this email. A maximum of 10 categories can be assigned to an email. Duplicate categories will be ignored and only unique entries will be sent.

Example

email
|> with_categories("campaign-12345")
Link to this function

with_google_analytics(email, enabled, utm_params \\ %{})

View Source

Instruct SendGrid to enable or disable Google Analytics tracking, and optionally set the UTM parameters for it. This is useful if you need to control UTM tracking parameters on an individual email basis.

Example

email
|> with_google_analytics(true, %{utm_source: "email", utm_campaign: "campaign"})

email
|> with_google_analytics(false)
Link to this function

with_ip_pool_name(email, ip_pool_name)

View Source

Specify the ip pool name.

Example

email
|> with_ip_pool_name("my-ip-pool-name")
Link to this function

with_send_at(email, time)

View Source

Specs

with_send_at(
  %Bamboo.Email{
    assigns: term(),
    attachments: term(),
    bcc: term(),
    blocked: term(),
    cc: term(),
    from: term(),
    headers: term(),
    html_body: term(),
    private: term(),
    subject: term(),
    text_body: term(),
    to: term()
  },
  %DateTime{
    calendar: term(),
    day: term(),
    hour: term(),
    microsecond: term(),
    minute: term(),
    month: term(),
    second: term(),
    std_offset: term(),
    time_zone: term(),
    utc_offset: term(),
    year: term(),
    zone_abbr: term()
  }
  | integer()
) :: %Bamboo.Email{
  assigns: term(),
  attachments: term(),
  bcc: term(),
  blocked: term(),
  cc: term(),
  from: term(),
  headers: term(),
  html_body: term(),
  private: term(),
  subject: term(),
  text_body: term(),
  to: term()
}

Schedule a time for SendGrid to deliver the email.

Note that if the time is in the past, SendGrid will immediately deliver the email.

Example

{:ok, delivery_time, _} = DateTime.from_iso8601("2020-01-01T00:00:00Z")

email
|> with_send_at(delivery_time)
Link to this function

with_template(email, template_id)

View Source

Specify the template for SendGrid to use for the context of the substitution tags.

Example

email
|> with_template("80509523-83de-42b6-a2bf-54b7513bd2aa")
Link to this function

with_unique_args(email, unique_args)

View Source

A map of unique arguments for this email. This will override any existing unique arguments.

Example

email
|> with_unique_args(%{new_arg_1: "new arg 1", new_arg_2: "new arg 2"})