Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Unable to send messages using different postmark servers #8

Closed
mikelarkin opened this Issue · 2 comments

3 participants

Michael Larkin Richard Smith Artem Chistyakov
Michael Larkin

I would like to send certain emails from a specific postmark "server". So, I have a method that gets called to set the ActionMailer::Base.postmark_api_key before each email goes out:

def set_mail_server(type=:account)
    case type
    when :order
      ActionMailer::Base.postmark_api_key = "1234"
    when :update
      ActionMailer::Base.postmark_api_key = "5678"
    else
      ActionMailer::Base.postmark_api_key = "9101"
    end
  end

And an example mail method:

  def invoice(invoice)
    default_options
    set_mail_server(:account)
    recipients invoice.email
    subject "Monthly Invoice"
    body :invoice => invoice
    tag "Account Invoice"
  end

The method is being called and the correct key is being set (verified via puts statements), however, Postmark actually sends from the same server if more than one message in the same request. Let's say I have 1 message from the "order" server and another from the "account" server that are sent during the same request. Postmark would send both from either the "order" or "account" server, even though I'm setting the key before each message is sent.

I've dug through the code and I can't seem to figure out where the value is being cached. I've also tried resetting the parameter to nil before setting it, but that doesn't work either.

Any ideas? Thanks!

Artem Chistyakov
Owner

It’s supported as of version 0.5.0. It became safe to override postmark_settings in ActionMailer::Base child classes.

ActionMailer::Base.postmark_settings = {api_key: 'your-global-api-key'} 

class OrderMailer < ActionMailer::Base
  self.postmark_settings = {api_key: 'orders-server-key'}
  default from: "orders@example.org"

  def order_email
    mail to: 'customer@example.org', subject: "Your order #00230067 has been accepted"
  end
end

class ReceiptMailer < ActionMailer::Base
  default from: "receipts@example.org"

  def receipt_email
    mail to: 'customer@example.org', subject: "Your receipt #00230067"
  end
end

ActionMailer::Base.postmark_settings
# => {:api_key=>"your-global-api-key"} 
OrderMailer.postmark_settings
# => {:api_key=>"orders-server-key"} 
ReceiptMailer.postmark_settings
# => {:api_key=>"your-global-api-key"}

OrderMailer.order_email.delivery_method
# => #<Mail::Postmark:0x007f9a9f400420 @settings={:api_key=>"orders-server-key"}> 
ReceiptMailer.receipt_email.delivery_method
# => #<Mail::Postmark:0x007f9a9f500c58 @settings={:api_key=>"your-global-api-key"}> 
Artem Chistyakov temochka closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.