`
xxj
  • 浏览: 434732 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

[Rails 常用插件简介]action_mailer_tls

阅读更多
感谢gigix推荐这么实用的插件:http://www.iteye.com/topic/36627#296988记录下来

因为在项目中用到gmail,而gmail的smtp又是带ssl认证的,Rails的Action Mailer是实现不了的。如果你也碰到这样的问题,或者也打算用gmail smtp,这个插件也许是你想要的。

0:前提
还没了解ActionMailer?那就先看看这里
ActionMailer:http://wiki.rubyonrails.org/rails/pages/ActionMailer


1:安装
ruby script/plugin install http://svn.nanorails.com/plugins/action_mailer_tls


2:使用
和你平时一样,不需要额外做什么

3:原理
我们来看下它的代码,很简单,就一个文件,覆盖了ActionMailer的do_start方法

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

    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
...
end

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics