Created
June 25, 2012 14:11
-
-
Save airblade/2988877 to your computer and use it in GitHub Desktop.
Monkey-patch enabling Rails 3.0 - 3.1 to use SMTP over SSL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/email.rb | |
require 'mail' | |
# Monkey-patch Mail to support SSL, which Fastmail mandates by 30 June 2012. | |
# | |
# Mail introduced SSL support in v2.4.0. | |
# Rails / ActionMailer doesn't use that version of Mail until Rails 3.2.0. | |
# | |
# The code below is copied from Mail v2.4.4. | |
# | |
# TODO: remove this monkey-patch once we're on Rails >= 3.2.0. | |
module Mail | |
class SMTP | |
# Send the message via SMTP. | |
# The from and to attributes are optional. If not set, they are retrieve from the Message. | |
def deliver!(mail) | |
# Set the envelope from to be either the return-path, the sender or the first from address | |
envelope_from = mail.return_path || mail.sender || mail.from_addrs.first | |
if envelope_from.blank? | |
raise ArgumentError.new('A sender (Return-Path, Sender or From) required to send a message') | |
end | |
destinations ||= mail.destinations if mail.respond_to?(:destinations) && mail.destinations | |
if destinations.blank? | |
raise ArgumentError.new('At least one recipient (To, Cc or Bcc) is required to send a message') | |
end | |
message ||= mail.encoded if mail.respond_to?(:encoded) | |
if message.blank? | |
raise ArgumentError.new('A encoded content is required to send a message') | |
end | |
smtp = Net::SMTP.new(settings[:address], settings[:port]) | |
if settings[:tls] || settings[:ssl] | |
if smtp.respond_to?(:enable_tls) | |
unless settings[:openssl_verify_mode] | |
smtp.enable_tls | |
else | |
openssl_verify_mode = settings[:openssl_verify_mode] | |
if openssl_verify_mode.kind_of?(String) | |
openssl_verify_mode = "OpenSSL::SSL::VERIFY_#{openssl_verify_mode.upcase}".constantize | |
end | |
context = Net::SMTP.default_ssl_context | |
context.verify_mode = openssl_verify_mode | |
smtp.enable_tls(context) | |
end | |
end | |
elsif settings[:enable_starttls_auto] | |
if smtp.respond_to?(:enable_starttls_auto) | |
unless settings[:openssl_verify_mode] | |
smtp.enable_starttls_auto | |
else | |
openssl_verify_mode = settings[:openssl_verify_mode] | |
if openssl_verify_mode.kind_of?(String) | |
openssl_verify_mode = "OpenSSL::SSL::VERIFY_#{openssl_verify_mode.upcase}".constantize | |
end | |
context = Net::SMTP.default_ssl_context | |
context.verify_mode = openssl_verify_mode | |
smtp.enable_starttls_auto(context) | |
end | |
end | |
end | |
response = nil | |
smtp.start(settings[:domain], settings[:user_name], settings[:password], settings[:authentication]) do |smtp_obj| | |
response = smtp_obj.sendmail(message, envelope_from, destinations) | |
end | |
return settings[:return_response] ? response : self | |
end | |
end | |
end | |
ActionMailer::Base.smtp_settings = { | |
address: 'mail.messagingengine.com', | |
port: 465, | |
domain: 'yourdomain.com', | |
user_name: 'your_username', | |
password: 'xxxxxxx', | |
authentication: :plain, | |
enable_starttls_auto: false, | |
ssl: true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment