How do you verify that the credentials of the Rails mail system are valid?

I have send mail through a GMail account, and I want to check if ActionMailer can really log into the GMail SMTP server with the credentials that I gave it. What is the best way to test this?

+5
source share
2 answers

This is not a complete stack solution, but you can verify that server authentication is working correctly using Net :: SMTP directly. The mailstone that Rails 3 uses to send ActionMailer emails uses Mail like this (with your ActionMailer.smtp_settings):

  #line 96 of mail-2.2.7/lib/mail/network/delivery_methods/smtp.rb
  smtp = Net::SMTP.new(settings[:address], settings[:port])
  if settings[:enable_starttls_auto]
    smtp.enable_starttls_auto if smtp.respond_to?(:enable_starttls_auto) 
  end

  smtp.start(settings[:domain], settings[:user_name], settings[:password],
   settings[:authentication]) do |smtp|
     smtp.sendmail(message, envelope_from, destinations)
     # @Mason: this line need not be included in your code. SMTP#start throws
     # a Net::SMTPAuthenticationError if the authentication was not successful.
     # So just putting this call to #start with an empty block in a method and
     # calling assert_no_raise Net::SMTPAuthenticationError should do the trick.
     # The empty block is necessary so that the connection gets closed.
     # Reference #{rubydir}/lib/ruby/1.8/net/smtp.rb for more info.
  end

, ActionMailer:: Base.smtp_settings :

  settings = ActionMailer::Base.smtp_settings

, , .

+9
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

authenticate Net::SMTPAuthenticationError, .

Net::SMTP::Response, status "235".

+5

All Articles