Skip to content

Email Sending

Jens Balvig edited this page Sep 18, 2023 · 7 revisions

Sending attachments

You can send file attachments with Postmark. Read our Developer docs for additional information.

The Postmark gem is compatible with ActionMailer attachments API. It allows you to specify the name, content-type and other attributes for your attachments.

The legacy :postmark_attachments attribute is no longer supported on Rails 3.2.13 and above.

class TestMailer < ActionMailer::Base

  def message_with_attachment
    attachments.inline['logo.png'] = File.read("/path/to/image") # Inline image
    attachments['42.jpg'] = File.read("/path/to/file") # Attached file
    mail(
      :subject              => 'hello',
      :to                   => 'sheldon@bigbangtheory.com',
      :from                 => 'leonard@bigbangtheory.com'
    )
  end

end

Attaching metadata to messages

Postmark supports attaching metadata to messages. All metadata field values will be interpreted and returned in webhook payloads as strings.

class TestMailer < ActionMailer::Base

  def message_with_metadata
    metadata['foo'] = 'bar'
    mail(
      :subject              => 'meta hello',
      :to                   => 'sheldon@bigbangtheory.com',
      :from                 => 'leonard@bigbangtheory.com'
    )
  end

end

Sending in batches

While Postmark is focused on transactional email, we understand that developers with higher volumes or processing time constraints need to send their messages in batches. To facilitate this we provide a batching endpoint that permits you to send up to 500 well-formed Postmark messages in a single API call.

client = Postmark::ApiClient.new('your-api-token')

messages = []
messages << DigestMailer.weekly_digest(@user1)
messages << DigestMailer.weekly_digest(@user2)

client.deliver_messages(messages)

messages.first.delivered?
# => true

messages.all?(&:delivered)
# => true

Tracking opens and tagging your deliveries

You can use tags to categorize outgoing messages and attach application-specific information. Tagging the different types of email that you send lets you review statistics and bounce reports separately.

Pass :track_opens => 'true' to enable/disable open tracking on per-message basis. Check out the Triggers API to see how Postmark can help you control this with tags. Note that we pass a string here, since it becomes a header value. Passing a boolean may or may not work depending on your Rails version.

class TestMailer < ActionMailer::Base

  def tagged_message
    mail(
      :subject => 'hello',
      :to      => 'sheldon@bigbangtheory.com',
      :from    => 'leonard@bigbangtheory.com',
      :tag     => 'my-tag',
      :track_opens => 'true'
    )
  end

end

More examples

For more advanced examples, we suggest visiting Postmark Ruby Gem wiki pages.