이메일을 보내려고 할 때 "starttls 명령을 먼저 발행해야합니다"
-
08-07-2019 - |
문제
사용하는 동안 오류가 발생합니다 action_mailer_tls
내 레일 앱에서 Gmail과 통신하기 위해 플러그인 :
Must issue a STARTTLS command first
다른 사람들은 만난 것 같습니다 이 같은 문제:
문제는 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 = {
- : 주소 => "smtp.gmail.com",
- : port => 587,
- : domain => "domain.com",
- : user_name => "user@domain.com",
- : password => "password",
- : Authentication => : 일반
- }
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
구성/환경 .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
옵션. 샘플 구성 (생산 환경에서)은 다음과 같습니다.
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=lessSecureAppaccess
추신 : 위의 링크에서 도메인 이름을 변경하는 것을 잊지 마십시오.
이 Heps를 바랍니다 !!
나는 레일 2.3.4를 실행하고있다. 그리고 (Googling에서) 생각했지만 플러그인이 필요하지 않았고 라인 만 필요하다고 생각했다.
: enable_starttls_auto => true,
나는 실제로 Ski가 게시 한 Alexander Pomozov 솔루션을 사용했을 때만 작동했습니다. 이유에 대한 의견이 있습니까? 좋을 것이지만 나는 그것이 작동하는 것이 행복합니다.