在尝试发送电子邮件时获取“必须首先发出STARTTLS命令”
我在尝试使用action_mailer_tls
插件与我的Rails应用中的Gmail通信时遇到错误:
Must issue a STARTTLS command first
其他人似乎遇到了同样的问题 :
问题是Gmail需要TLS身份validation,但标准的Ruby net / smtp库不支持TLS。
本文建议按照以下步骤操作:
当然,Marc Chung创建了一个有用的插件来克服这个障碍。 您可以在此处找到它并手动将其添加到项目中,也可以将其导出到插件目录中。
$ cd vendor/plugins
$ svn export http://code.openrain.com/rails/action_mailer_tls/
无论哪种方式,请确保您需要’smtp_tls’
现在你需要的是更新你的smtp_settings,如果你还没有这样做的话。
- ActionMailer :: Base.smtp_settings = {
- :address =>“smtp.gmail.com”,
- :port => 587,
- :domain =>“domain.com”,
- :user_name =>“user@domain.com”,
- :password =>“密码”,
- :authentication =>:plain
- }
任何有关与Gmail通信的更好解决方案的建议都将受到赞赏。
我使用Alexander Pomozov的解决方案从我的Rails应用程序与Gmail交谈。 我相信原始文章已经消失,但有人在此处复制了Google缓存。
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
到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' }
正常使用ActionMailer。
使用Ruby 1.8.7和Rails 2.3.4(虽然已经有多个版本),但是通过使用:enable_starttls_auto
选项,我无需使用特定于TLS的ActionMailer插件就获得了成功。 示例配置(来自生产环境)如下所示:
ActionMailer::Base.smtp_settings = { :enable_starttls_auto => true, :address => "smtp.gmail.com", :port => 587, :domain => "domain.com", :authentication => :plain, :user_name => "username@domain", :password => "secret" }
我启用了starttls使用:enable_starttls_auto => true
但仍然得到相同的错误。 最后,我能够在不对代码进行单一更改的情况下解决它。 如果您使用smtp.gmail.com发送邮件,则必须首先允许安全性较低的应用程序使用您的电子邮件发送邮件。 为此,请使用您要从中发送邮件的帐户登录,然后转到此链接并启用对安全性较低的应用的访问权限。
编辑:如果您不允许将设置更改为安全性较低的应用程序,则应联系该域的管理员帐户持有者以更改设置,以允许用户使用安全性较低的应用程序。
如果您拥有管理员权限,则可以允许用户更改安全性较低的应用设置,您可以通过https://admin.google.com/domainname.com/AdminHome#ServiceSettings/notab=1&service=securitysetting&subtab=lesssecureappsaccess进行更改
PS:不要忘记在上面的链接中更改域名。
希望这个好!
我正在运行rails 2.3.4虽然我认为(从谷歌搜索)你不需要任何插件,只需要线
:enable_starttls_auto => true,
实际上,当我使用上面滑雪发布的亚历山大Pomozov解决方案时,我才能使用它(对你们来说很重要)。 有什么评论为什么? 会很棒,但我很高兴它有效。