获取“必须首先发出STARTTLS命令”当试图发送电子邮件时 [英] Getting "Must issue a STARTTLS command first" when trying to send email

查看:217
本文介绍了获取“必须首先发出STARTTLS命令”当试图发送电子邮件时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用 action_mailer_tls 插件与我的Rails应用程序中的Gmail通信时收到错误消息:

I'm getting an error while trying to use the action_mailer_tls plugin to communicate with Gmail in my Rails app:

Must issue a STARTTLS command first

其他人似乎遇到同样的问题


问题是Gmail需要TLS
身份验证,但标准Ruby
net / smtp库不支持TLS。

The problem is that Gmail requires TLS authentication but the standard Ruby net/smtp library doesn't support TLS.

这篇文章建议您按照以下步骤操作:

The article recommends following these steps, which I did:


当然,Marc Chung创造了一个有用的插件
来克服这个
障碍。你可以在这里找到它,
手动将它添加到你的项目中,或者你
可以将它导出到你的插件
目录中。

Of course there is a helpful plugin created by Marc Chung to overcome this barrier. You can find it here and manually add it to your project or you can export it to your plugin directory.


  1. $ cd vendor / plugins

  2. $ svn导出http://代码。 openrain.com/rails/action_mailer_tls/

  1. $ cd vendor/plugins
  2. $ svn export http://code.openrain.com/rails/action_mailer_tls/

无论哪种方式确保您需要
'smtp_tls '

Either way make sure you require 'smtp_tls'

现在您只需要更新您的
smtp_settings,如果您尚未完成
的话。

Now all you need is to update your smtp_settings if you haven't done so already.


  1. ActionMailer :: Base.smtp_settings = {

  2. :address =>smtp.gmail.com,

  3. :port => 587,

  4. :domain =>domain.com,

  5. :user_name =>user @ domain.com,

  6. :password =>password,

  7. :authentication =>:


  1. ActionMailer::Base.smtp_settings = {
  2. :address => "smtp.gmail.com",
  3. :port => 587,
  4. :domain => "domain.com",
  5. :user_name => "user@domain.com",
  6. :password => "password",
  7. :authentication => :plain
  8. }


更好的Gmail解决方案建议赞赏。

Any suggestions for a better solution to talk to Gmail would be appreciated.

推荐答案

我使用Alexander Pomozov的解决方案从我的Rails应用程序与Gmail交谈。我相信原来的文章已经不存在了,但是有人已经复制了Google缓存 nofollow noreferrer>在这里。

I used Alexander Pomozov's solution to talk to Gmail from my Rails app. I believe the original article is gone but someone has reproduced the Google cache over here.

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



config / environment.rb



(添加所有其他内容后)

config/environment.rb

(add after everything else)

    require "smtp_tls"

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

正常使用ActionMailer。

Use ActionMailer as normal.

这篇关于获取“必须首先发出STARTTLS命令”当试图发送电子邮件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆