Actionmailer SMTP Server Response

前端 未结 2 1451
予麋鹿
予麋鹿 2021-01-12 00:41

When sending mail through actionmailer, the actionmailer gets a response from the SMTP server, when its ok, or when its wrong. Is there a way to retrieve this response after

相关标签:
2条回答
  • 2021-01-12 01:04

    Set return_response: true in the smtp settings and call message.deliver! instead of deliver. This returns the SMTP server response, a Net::SMTP::Response, which contains the server response you're looking for.

    If you need a log of all responses from the connection with the server, not just the final result, you'll need to dig into Net::SMTP.

    0 讨论(0)
  • 2021-01-12 01:12

    Looking at the the source you can define an observer:

    in base.rb

      # Register an Observer which will be notified when mail is delivered.
      # Either a class or a string can be passed in as the Observer. If a string is passed in
      # it will be <tt>constantize</tt>d.
      def register_observer(observer)
        delivery_observer = (observer.is_a?(String) ? observer.constantize : observer)
        Mail.register_observer(delivery_observer)
      end
    

    So you could use some code like this in an initialization file:

    class MailObserver
      def self.delivered_email(message)
        logger_info "Sent Message: #{message}"
      end
    end
    
    ActionMailer::Base.register_observer(MailObserver)
    

    That will log sent mail and you can see if you can get the headers or response from the sent mail object.

    0 讨论(0)
提交回复
热议问题