Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial migration

  • Loading branch information...
commit 4175db7171128f3f456e9ecd394998e3d98fe548 0 parents
Marc Chung mchung authored
19 README
... ... @@ -0,0 +1,19 @@
  1 += SMTP_TLS + ActionMailer + Gmail
  2 +
  3 +Configure Rails To Use Gmail's SMTP
  4 +
  5 +Originally from http://blog.pomozov.info/posts/how-to-send-actionmailer-mails-to-gmailcom.html
  6 +
  7 +See "sample" directory for further instructions
  8 +
  9 +== Resources
  10 +
  11 +Blog post
  12 +
  13 +* http://www.rubyinside.com/how-to-use-gmails-smtp-server-with-rails-394.html
  14 +
  15 +* http://www.prestonlee.com/archives/63
  16 +
  17 +== Author
  18 +
  19 +* Marc Chung - marc [dot] chung [at] openrain [dot] com
1  init.rb
... ... @@ -0,0 +1 @@
  1 +require_dependency 'smtp_tls'
65 lib/smtp_tls.rb
... ... @@ -0,0 +1,65 @@
  1 +require "openssl"
  2 +require "net/smtp"
  3 +
  4 +Net::SMTP.class_eval do
  5 + private
  6 + def do_start(helodomain, user, secret, authtype)
  7 + raise IOError, 'SMTP session already started' if @started
  8 + check_auth_args user, secret, authtype if user or secret
  9 +
  10 + sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
  11 + @socket = Net::InternetMessageIO.new(sock)
  12 + @socket.read_timeout = 60 #@read_timeout
  13 +
  14 + check_response(critical { recv_response() })
  15 + do_helo(helodomain)
  16 +
  17 + if starttls
  18 + raise 'openssl library not installed' unless defined?(OpenSSL)
  19 + ssl = OpenSSL::SSL::SSLSocket.new(sock)
  20 + ssl.sync_close = true
  21 + ssl.connect
  22 + @socket = Net::InternetMessageIO.new(ssl)
  23 + @socket.read_timeout = 60 #@read_timeout
  24 + do_helo(helodomain)
  25 + end
  26 +
  27 + authenticate user, secret, authtype if user
  28 + @started = true
  29 + ensure
  30 + unless @started
  31 + # authentication failed, cancel connection.
  32 + @socket.close if not @started and @socket and not @socket.closed?
  33 + @socket = nil
  34 + end
  35 + end
  36 +
  37 + def do_helo(helodomain)
  38 + begin
  39 + if @esmtp
  40 + ehlo helodomain
  41 + else
  42 + helo helodomain
  43 + end
  44 + rescue Net::ProtocolError
  45 + if @esmtp
  46 + @esmtp = false
  47 + @error_occured = false
  48 + retry
  49 + end
  50 + raise
  51 + end
  52 + end
  53 +
  54 + def starttls
  55 + getok('STARTTLS') rescue return false
  56 + return true
  57 + end
  58 +
  59 + def quit
  60 + begin
  61 + getok('QUIT')
  62 + rescue EOFError
  63 + end
  64 + end
  65 +end
6 sample/mailer.yml.sample
... ... @@ -0,0 +1,6 @@
  1 +---
  2 + :address: smtp.gmail.com
  3 + :port: 587
  4 + :user_name: email@dress.com
  5 + :password: h@ckme
  6 + :authentication: :plain
12 sample/smtp_gmail.rb
... ... @@ -0,0 +1,12 @@
  1 +#
  2 +# For Rails 2.x:
  3 +# A copy of this file should be placed in RAILS_ROOT/initializers/
  4 +# A file named mailer.yml should be placed in RAILS_ROOT/config/
  5 +# See mailer.yml.sample
  6 +#
  7 +
  8 +require "smtp_tls"
  9 +
  10 +mailer_config = File.open("#{RAILS_ROOT}/config/mailer.yml")
  11 +mailer_options = YAML.load(mailer_config)
  12 +ActionMailer::Base.smtp_settings = mailer_options

0 comments on commit 4175db7

Please sign in to comment.
Something went wrong with that request. Please try again.