Redmine:通过gmail错误SSL_connect或STARTTLS发送邮件

时间:2014-01-30 18:07:58

标签: ruby-on-rails ruby email gmail redmine

我尝试为电子邮件通知配置redmine

所以configuration.yml中的配置是:

production:
   email_delivery:
     delivery_method: :smtp
     smtp_settings:
       enable_starttls_auto: true
       address: "smtp.gmail.com"
       port: '587'
       domain: "smtp.gmail.com"
       authentication: :plain
       user_name: "mymail@gmail.com"
       password: "mypass"

根据本教程:

http://www.redmine.org/projects/redmine/wiki/EmailConfiguration#Simple-Login-Authentication-default-settings

但是当我尝试:发送测试邮件时,我有这个错误:

An error occurred while sending mail (530 5.7.0 Must issue a STARTTLS command first. n7sm25368265eef.5 - gsmtp )

如果我有:

production:
   email_delivery:
     delivery_method: :smtp
     smtp_settings:
       tls: true
       enable_starttls_auto: true
       address: "smtp.gmail.com"
       port: '587'
       domain: "smtp.gmail.com"
       authentication: :plain
       user_name: "mymail@gmail.com"
       password: "mypass"

我有这个错误:

An error occurred while sending mail (SSL_connect returned=1 errno=0 state=unknown state: unknown protocol)

有什么想法吗? Redmine版本:2.0.1,Ruby:1.9.3

1 个答案:

答案 0 :(得分:5)

1.在lib/smtp_tls.rb中的rails应用程序中保存以下代码:

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

    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)

    if starttls
      raise 'openssl library not installed' unless defined?(OpenSSL)
      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)
    end

    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') rescue return false
    return true
  end

  def quit
    begin
      getok('QUIT')
    rescue EOFError, OpenSSL::SSL::SSLError
    end
  end
end

2.将此代码添加到config/environment.rb(在其他所有内容之后):

require “smtp_tls”

ActionMailer::Base.smtp_settings = {
  :address => “smtp.gmail.com”,
  :port => 587,
  :authentication => :plain,
  :user_name => “someone@openrain.com”,
  :password => ’someonesPassword’
} 

3.正常使用ActionMailer。