how do you test that Rails mailer credentials are valid?

后端 未结 2 1474
清酒与你
清酒与你 2021-02-06 03:55

I\'ve got a mailer sending through a GMail account, and I want to test that ActionMailer can actually log in to GMail\'s SMTP server with the credentials I\'ve given it. What\'s

2条回答
  •  失恋的感觉
    2021-02-06 04:27

    smtp = Net::SMTP.new settings[:address], settings[:port]
    smtp.enable_starttls_auto if settings[:enable_starttls_auto]
    smtp.start(settings[:domain]) do
      expect {
        smtp.authenticate settings[:user_name], settings[:password], settings[:authentication]
      }.to_not raise_error
    end
    

    Calling authenticate will raise a Net::SMTPAuthenticationError if the authentication fails.

    Otherwise, it will return a Net::SMTP::Response, and calling status on the response will return "235".

提交回复
热议问题