Class: PostmarkClient::Resources::Emails

Inherits:
Client::Base show all
Defined in:
lib/postmark_client/resources/emails.rb

Overview

Email resource client for sending emails via the Postmark API.

Examples:

Sending a simple email

emails = PostmarkClient::Resources::Emails.new
response = emails.send_email(
  from: "[email protected]",
  to: "[email protected]",
  subject: "Hello!",
  text_body: "Hello, World!"
)

Sending an email with an Email model

email = PostmarkClient::Email.new(
  from: "[email protected]",
  to: "[email protected]",
  subject: "Hello!",
  html_body: "<h1>Hello, World!</h1>"
)
response = emails.send(email)

Sending with a custom API token

emails = PostmarkClient::Resources::Emails.new(api_token: "my-token")
response = emails.send(email)

Constant Summary

Constants inherited from Client::Base

Client::Base::API_BASE_URL

Instance Attribute Summary

Attributes inherited from Client::Base

#api_token, #options

Instance Method Summary collapse

Methods inherited from Client::Base

#initialize

Constructor Details

This class inherits a constructor from PostmarkClient::Client::Base

Instance Method Details

#send(email) ⇒ EmailResponse

Send a single email

Examples:

With Email model

response = emails.send(email)

With hash

response = emails.send({
  from: "[email protected]",
  to: "[email protected]",
  subject: "Test",
  text_body: "Hello"
})

Parameters:

  • email (Email, Hash)

    the email to send

Returns:

Raises:



47
48
49
50
51
52
53
# File 'lib/postmark_client/resources/emails.rb', line 47

def send(email)
  email = normalize_email(email)
  email.validate!

  response = post("/email", email.to_h)
  EmailResponse.new(response)
end

#send_batch(emails) ⇒ Array<EmailResponse>

Send a batch of emails (up to 500)

Examples:

emails_to_send = [
  { from: "[email protected]", to: "[email protected]", subject: "Hi", text_body: "Hello" },
  { from: "[email protected]", to: "[email protected]", subject: "Hi", text_body: "Hello" }
]
responses = client.send_batch(emails_to_send)

Parameters:

  • emails (Array<Email, Hash>)

    array of emails to send

Returns:

Raises:

  • (ValidationError)

    if any email is invalid

  • (ApiError)

    if the API returns an error

  • (ArgumentError)

    if batch exceeds 500 emails



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/postmark_client/resources/emails.rb', line 106

def send_batch(emails)
  raise ArgumentError, "Batch cannot exceed 500 emails" if emails.length > 500

  normalized = emails.map { |e| normalize_email(e) }
  normalized.each(&:validate!)

  payload = normalized.map(&:to_h)
  responses = post("/email/batch", payload)

  responses.map { |r| EmailResponse.new(r) }
end

#send_email(from:, to:, subject:, **kwargs) ⇒ EmailResponse

Convenience method to send an email with parameters

Examples:

response = emails.send_email(
  from: "[email protected]",
  to: "[email protected]",
  subject: "Hello!",
  text_body: "Hello, World!",
  track_opens: true
)

Parameters:

  • from (String)

    sender email address

  • to (String, Array<String>)

    recipient email address(es)

  • subject (String)

    email subject

  • kwargs (Hash)

    additional email options

Options Hash (**kwargs):

  • :html_body (String)

    HTML email body

  • :text_body (String)

    plain text email body

  • :cc (String)

    CC recipients

  • :bcc (String)

    BCC recipients

  • :reply_to (String)

    reply-to address

  • :tag (String)

    email tag

  • :track_opens (Boolean)

    whether to track opens

  • :track_links (String)

    link tracking setting

  • :metadata (Hash)

    custom metadata

  • :message_stream (String)

    message stream

Returns:



81
82
83
84
85
86
87
88
89
# File 'lib/postmark_client/resources/emails.rb', line 81

def send_email(from:, to:, subject:, **kwargs)
  email = Email.new(
    from: from,
    to: to,
    subject: subject,
    **kwargs
  )
  send(email)
end