Powered by AppSignal & Oban Pro

BentoSdk Emails API

livebook/emails_api.livemd

BentoSdk Emails API

Mix.install([
  {:bento_sdk, "~> 0.1.0"}
])

Introduction

This notebook demonstrates how to use the BentoSdk to send emails through the Bento marketing platform. The Emails API allows you to:

  • Send regular emails
  • Send transactional emails
  • Send bulk emails (multiple emails in one request)

Configuration

We’ll use Livebook’s secrets feature to securely store and access your Bento credentials:

# Configure BentoSdk with the secrets
BentoSdk.configure(
  site_uuid: System.fetch_env!("LB_BENTO_SITE_UUID"),
  username: System.fetch_env!("LB_BENTO_USERNAME"),
  password: System.fetch_env!("LB_BENTO_PASSWORD")
)

Email Validation

The Emails module includes email validation to ensure that email addresses are properly formatted:

# Valid email
try do
  BentoSdk.Emails.validate_email("user@example.com")
  "Email is valid"
rescue
  e in ArgumentError -> "Error: #{e.message}"
end
# Invalid email
try do
  BentoSdk.Emails.validate_email("invalid-email")
  "Email is valid"
rescue
  e in ArgumentError -> "Error: #{e.message}"
end

Sending Regular Emails

Let’s send a regular email:

# Example of sending a regular email
to_email = "user@example.com"
from_email = "sender@example.com"
subject = "Hello from BentoSdk"
html_body = "<h1>Hello!</h1><p>This is a test email from BentoSdk.</p>"
personalizations = %{
  "first_name" => "John",
  "last_name" => "Doe"
}

case BentoSdk.Emails.send(to_email, from_email, subject, html_body, personalizations) do
  {:ok, result} ->
    result
  {:error, reason} ->
    "Error: #{reason}"
end

Sending Transactional Emails

You can send transactional emails:

# Example of sending a transactional email
to_email = "recipient@example.com"
from_email = "sender@example.com"
subject = "Your Order Confirmation"
html_body = "<h1>Thank you for your order!</h1><p>Your order #12345 has been confirmed.</p>"
personalizations = %{
  "order_id" => "12345",
  "customer_name" => "John Doe",
  "total" => "$99.99"
}

case BentoSdk.Emails.send_transactional(to_email, from_email, subject, html_body, personalizations) do
  {:ok, result} ->
    result
  {:error, reason} ->
    "Error: #{reason}"
end

Sending Bulk Emails

You can send multiple emails in a single request:

# Example of sending multiple emails in a batch
emails = [
  %{
    to: "user1@example.com",
    from: "noreply@yourdomain.com",
    subject: "Welcome to our service",
    html_body: "<h1>Welcome!</h1><p>Thanks for signing up.</p>",
    personalizations: %{"first_name" => "John"}
  },
  %{
    to: "user2@example.com",
    from: "noreply@yourdomain.com",
    subject: "Your order has shipped",
    html_body: "<h1>Order Shipped</h1><p>Your order #123 has shipped.</p>",
    personalizations: %{"first_name" => "Jane", "order_number" => "123"},
    transactional: true
  }
]

case BentoSdk.Emails.send_bulk(emails) do
  {:ok, result} ->
    result
  {:error, reason} ->
    "Error: #{reason}"
end