ruby-on-rails - 红矿: send mail via gmail error SSL_connect or STARTTLS

标签 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.将以下代码保存在您的 Rails 应用中的 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

    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。

关于ruby-on-rails - 红矿: send mail via gmail error SSL_connect or STARTTLS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21464238/

相关文章:

email - 邮件列表中的汉字显示为问号?

ruby-on-rails - Ruby on Rails,我可以获得 Mongoid 池中当前的连接数吗?

javascript - Rails 4.2 jQuery 仅在刷新后加载

ruby-on-rails - RSpec: stub 私有(private)方法

ruby-on-rails - 没有模型的 Google-maps-for-Rails

php - 担心使用 MailChimp 发送成员(member)信息

ruby-on-rails - Firefox 中的确认弹出错误是否有解决方法?

ruby-on-rails - 如何从 Ruby 运行非阻塞命令?

ruby - 如何从数组中删除一系列值?

javascript - 如何覆盖电子邮件输入验证器消息?