gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:34:27 25 4
gpt4 key购买 nike

我尝试为电子邮件通知配置 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com