1

How could I get the response from an SMTP server in Ruby on Rails using ActionMailer, when I send an email with the Mailer.deliver method?

I found the Actionmailer SMTP Server Response answer but it doesn't work... Any idea?

I need it because AWS SES return a message ID through, and it's the only way to get the message linked to a bounce or spam report they provide after.

Community
  • 1
  • 1
Capripot
  • 1,354
  • 16
  • 26

2 Answers2

3

if you get the smtp in way like this

response = Mail.your_mail().deliver

and it isn't getting the response that you need, try deliver_now!

response = Mail.your_mail().deliver_now!
pp response.message

This will show you the result: 250 2.0.0 Ok: queued as 3lG8S16Khmz6ZhXq

I also configured the smtp settings on my Mail.rb

delivery_options = {   user_name: 'email@server.com.br',
                       password: 'mypassword',
                       authentication: :login,
                       address: 'smtp.mycompany.com.br',
                       port: 2525,
                       domain: 'mycompany.com.br',
                       return_response: true
                    }
Pedro Ivan
  • 322
  • 1
  • 14
  • Does this " Ok: queued as ..." message means that the e-mail was successfully delivered for sure? – carla Apr 28 '16 at 21:34
1

If using gem "aws-ses" you can get the message id with:

response = your_mailer.send(notification, self, *args).deliver_now!
response.message_id # => "010601531c886c55-6c2f30fc-4237-4294-9029-b60b2d44a691-000000@email.amazonses.com"

For me deliver, deliver_now and deliver_now! work in the same way

EXTRA: If for some chance you are using devise you can get the ID too:

#[app/model/user.rb]
class User < ActiveRecord::Base
  ...
  # Override send_devise_notification stated in devise/REAMDE.txt
  def send_devise_notification(notification, *args)                      
    response = devise_mailer.send(notification, self, *args).deliver_now
    response.message_id #=> "010601531c8-86c...000000@email.amazonses.com"
  end
  ...
end
ivanxuu
  • 842
  • 9
  • 10
  • While using `config.action_mailer.delivery_method = :aws_sdk` the code `response.message_id` did return the id, thanks =) – cavpollo Jul 13 '16 at 01:13