我在尝试使用action_mailer_tls插件在我的Rails应用中与Gmail通信时遇到错误:
Must issue a STARTTLS command first
Run Code Online (Sandbox Code Playgroud)
其他人似乎遇到了同样的问题:
问题是Gmail需要TLS身份验证,但标准的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交谈的更好解决方案的建议都将受到赞赏.
小智 10
我使用Alexander Pomozov的解决方案从我的Rails应用程序与Gmail交谈.我相信原始文章已经消失,但有人在此处复制了Google缓存.
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
Run Code Online (Sandbox Code Playgroud)
(添加其他所有内容后)
require “smtp_tls”
ActionMailer::Base.smtp_settings = {
:address => “smtp.gmail.com”,
:port => 587,
:authentication => :plain,
:user_name => “someone@openrain.com”,
:password => ’someonesPassword’
}
Run Code Online (Sandbox Code Playgroud)
正常使用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"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4966 次 |
| 最近记录: |