Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Response from AWS SES with Rails ActionMailer

How do I get message-id from AWS SES when using Rails ActionMailer with :aws_sdk as a delivery method?

config.action_mailer.delivery_method = :aws_sdk

I am only getting the message-id set by ActionMailer, but that gets overwritten by SES:

response = ApplicationMailer.create_send(@message).deliver_now
puts response.message_id # => [email protected]

How to get the response from SES with actual message-id set by SES? I found this Response from SMTP server with Rails, but it didn't work for me.

like image 246
ZeroEnna Avatar asked Feb 01 '26 02:02

ZeroEnna


1 Answers

I'm not an expert in ruby but I think the problem is with the AWS library. Specifically with the fact that there is no way to set the return_response setting.

In this file gems/2.3.0/gems/aws-sdk-rails-1.0.1/lib/aws/rails/mailer.rb

settings is a function hard coded to return an empty hash. And there is no attr_accessor and there is no initialize for it either.

  # ActionMailer expects this method to be present and to return a hash.
  def settings
    {}
  end

And in message.rb there you can get the smtp response code only if delivery_method.settings[:return_response] is true.

# This method bypasses checking perform_deliveries and raise_delivery_errors,
# so use with caution.
#
# It still however fires off the interceptors and calls the observers callbacks if they are defined.
#
# Returns self
def deliver!
  inform_interceptors
  response = delivery_method.deliver!(self)

  inform_observers
  delivery_method.settings[:return_response] ? response : self

end

But because settings is not accessible, there is no way to get the response.

So maybe it is possible to use a ruby trick like prepend to override the settings method in the aws library, but for now I'm just adding this line to the aws library file.

  # ActionMailer expects this method to be present and to return a hash.
  def settings
    { :return_response => true }
  end
like image 55
Zachary Spath Avatar answered Feb 03 '26 19:02

Zachary Spath