Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added original plugin with a README to get the facts straight.

  • Loading branch information...
commit dff7bc77e233667667d3e56c24631bab73f9d99b 0 parents
dougal authored
32 README
@@ -0,0 +1,32 @@
+= action_mailer_optional_tls
+
+Allows Google Mail SMTP to be used with Ruby on Rails' ActionMailer.
+
+== Install
+ ./script/plugin install http://svn.douglasfshearer.com/rails/plugins/action_mailer_optional_tls
+
+== Confugure
+
+Add the following to your environment.rb file:
+
+ ActionMailer::Base.smtp_settings = {
+ :tls => true,
+ :address => "smtp.gmail.com",
+ :port => "587",
+ :domain => "YOURDOMAIN",
+ :authentication => :plain,
+ :user_name => "GOOGLEUSERNAME",
+ :password => "GOOGLEPASSWORD"
+ }
+
+== Webified Documentation
+
+http://douglasfshearer.com/blog/gmail-smtp-with-ruby-on-rails-and-actionmailer
+
+== Credits
+
+This useful plugin is hosted at http://douglasfshearer.com as a public service, due to the continued downtime of http://kylemaxwell.com
+
+Douglas F Shearer is neither the creator nor maintainer of this work, so if it's broke, it's broke.
+
+Created by Kyle Maxwell - http://kylemaxwell.com/
13 Rakefile
@@ -0,0 +1,13 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
5 init.rb
@@ -0,0 +1,5 @@
+# Cobbled together from miscellaneous code from Stephen Chu and the ruby-talk list.
+#
+
+require_dependency 'smtp_tls'
+require_dependency 'action_mailer_tls'
14 lib/action_mailer_tls.rb
@@ -0,0 +1,14 @@
+require "rubygems"
+gem "actionmailer"
+ActionMailer::Base.class_eval do
+ private
+ def perform_delivery_smtp(mail)
+ destinations = mail.destinations
+ mail.ready_to_send
+
+ Net::SMTP.start(smtp_settings[:address], smtp_settings[:port], smtp_settings[:domain],
+ smtp_settings[:user_name], smtp_settings[:password], smtp_settings[:authentication], smtp_settings[:tls]) do |smtp|
+ smtp.sendmail(mail.encoded, mail.from, destinations)
+ end
+ end
+end
90 lib/smtp_tls.rb
@@ -0,0 +1,90 @@
+require "openssl"
+require "net/smtp"
+
+Net::SMTP.class_eval do
+
+ def self.start( address, port = nil,
+ helo = 'localhost.localdomain',
+ user = nil, secret = nil, authtype = nil, use_tls = false,
+ &block) # :yield: smtp
+ new(address, port).start(helo, user, secret, authtype, use_tls, &block)
+ end
+
+ def start( helo = 'localhost.localdomain',
+ user = nil, secret = nil, authtype = nil, use_tls = false ) # :yield: smtp
+ start_method = use_tls ? :do_tls_start : :do_start
+ if block_given?
+ begin
+ send start_method, helo, user, secret, authtype
+ return yield(self)
+ ensure
+ do_finish
+ end
+ else
+ send start_method helo, user, secret, authtype
+ return self
+ end
+ end
+
+ private
+
+ def do_tls_start(helodomain, user, secret, authtype)
+ raise IOError, 'SMTP session already started' if @started
+ check_auth_args user, secret, authtype if user or secret
+
+ sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
+ @socket = Net::InternetMessageIO.new(sock)
+ @socket.read_timeout = 60 #@read_timeout
+ @socket.debug_output = STDERR #@debug_output
+
+ check_response(critical { recv_response() })
+ do_helo(helodomain)
+
+ raise 'openssl library not installed' unless defined?(OpenSSL)
+ starttls
+ ssl = OpenSSL::SSL::SSLSocket.new(sock)
+ ssl.sync_close = true
+ ssl.connect
+ @socket = Net::InternetMessageIO.new(ssl)
+ @socket.read_timeout = 60 #@read_timeout
+ @socket.debug_output = STDERR #@debug_output
+ do_helo(helodomain)
+
+ authenticate user, secret, authtype if user
+ @started = true
+ ensure
+ unless @started
+ # authentication failed, cancel connection.
+ @socket.close if not @started and @socket and not @socket.closed?
+ @socket = nil
+ end
+ end
+
+ def do_helo(helodomain)
+ begin
+ if @esmtp
+ ehlo helodomain
+ else
+ helo helodomain
+ end
+ rescue Net::ProtocolError
+ if @esmtp
+ @esmtp = false
+ @error_occured = false
+ retry
+ end
+ raise
+ end
+ end
+
+ def starttls
+ getok('STARTTLS')
+ end
+
+ def quit
+ begin
+ getok('QUIT')
+ rescue EOFError, OpenSSL::SSL::SSLError
+ end
+ end
+end
37 test/tls_test.rb
@@ -0,0 +1,37 @@
+require "rubygems"
+require 'test/unit'
+require_gem "actionpack"
+require_gem "actionmailer"
+require "init"
+
+class Emailer < ActionMailer::Base
+ def email(h)
+ recipients h[:recipients]
+ subject h[:subject]
+ from h[:from]
+ part :content_type => "text/plain", :body => h[:body]
+ end
+end
+
+class TlsTest < Test::Unit::TestCase
+ # Replace this with your real tests.
+ def setup
+ ActionMailer::Base.smtp_settings = {
+ :address => "smtp.gmail.com",
+ :port => 587,
+ :user_name => ENV['EMAIL'],
+ :password => ENV['PASSWORD'],
+ :authentication => :plain,
+ :tls => true
+ }
+ end
+
+ def test_send_mail
+ Emailer.deliver_email(
+ :recipients => ENV["EMAIL"],
+ :subject => "SMTP/TLS test",
+ :from => ENV["EMAIL"],
+ :body => "This email was sent at #{Time.now.inspect}"
+ )
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.