由于action_mailer本身并不支持tls或者ssl的链接方式,而gmail的smtp server又要求tls或者ssl方式验证用户名和密码,所以我们需要hack action_mailer让他支持tls/ssl。在网上找到一篇很有用的文章:http://www.stephenchu.com/2006/06/how-to-use-gmail-smtp-server-to-send.html

由于有可能链接在国内没办法打开所以将内容一并转下来,以备日后reference:

Rail’s ActionMailer was simply the automatic choice, since I am building a rails app.

Turns out GMail supports only SSL SMTP mailing service, meaning if you cannot create a SSL connection to its SMTP server, you cannot send email through them. My Rails and Ruby (1.84) version do not yet support creating a SSL SMTP connection through Net::SMTP. DHH writes about how to do so through installing msmtp here, but we developers just obviously love options =)

The dynamic nature of Ruby allows me to enhance the functionality of that class. I found the following code from a couple Japanese posts here and here on the web (fairly low Google ranking). Pasting them in my /vendor/plugins as follow:

$ cat vendor/plugins/action_mailer_tls/init.rb
require_dependency ‘smtp_tls’

$ cat vendor/plugins/action_mailer_tls/lib/smtp_tls.rb
require “openssl”
require “net/smtp”

Net::SMTP.class_eval do
private
def do_start(helodomain, user, secret, authtype)
raise IOError, ‘SMTP session already started’ if @started
check_auth_args user, secret, authtype if user or secret
end

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
ensure
  unless @started
    # authentication failed, cancel connection.
      @socket.close if not @started and @socket and not @socket.closed?
    @socket = nil
  end
end
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
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
  end
end

So now, in your ActionMailer::Base’s server settings if you have:

ActionMailer::Base.server_settings = {
:address => “smtp.gmail.com”,
:port => 587,
:domain => “mycompany.com”,
:authentication => :plain,
:user_name => “username”,
:password => “password”
}

You call your ActionMailer::Base’s deliver method (perhaps from a custom subclass), it will send an email through GMail. Mission accomplished.