Skip to content

Instantly share code, notes, and snippets.

@ardeearam
Last active December 1, 2016 04:53
Show Gist options
  • Select an option

  • Save ardeearam/84d2de637b70014a6bc24a87d7c02518 to your computer and use it in GitHub Desktop.

Select an option

Save ardeearam/84d2de637b70014a6bc24a87d7c02518 to your computer and use it in GitHub Desktop.
rails_test_smtp.rb
#start of with a (hopefully) working Rails Settings
settings = ActionMailer::Base.smtp_settings
#Change credentials that you want to test
settings[:user_name] = "new_username@example.com"
settings[:password] = "Password"
settings[:key] = "Password" #For TLS, this is usually the same with password
envelope_from = "new_username@example.com" #For most SMTP servers, this should be consistent
destinations = "destination@example.com,destination2@example.com"
message = <<< MESSAGE
From: new_username@example.com
To: destination@example.com
Subject: Test message
This is a test message.
MESSAGE
#http://stackoverflow.com/a/6098918/95552
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment