2009-04-02 34 views
8

Todo lo que quiero es enviar correos electrónicos desde mis scripts de Ruby, a través de SMTP utilizando SSL.Cómo enviar correo con ruby ​​sobre smtp con ssl (no con rieles, sin TLS para gmail)

Solo encuentro ejemplos de hacerlo desde Rails, o para Gmail con TLS.

Encontré gente hablando de soporte SMTPS con ruby ​​1.8.5, pero el libdoc no lo menciona.

¿Alguien con un ejemplo de envío de correo a través de SMTP con SSL, en el puerto 465?

ruby -v 
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux] 

Respuesta

0

Usted probablemente ya sabe acerca de la Net::SMTP standard library

En cuanto a la parte SSL, lo que no parece ser apoyado fuera de la caja, me encontré con un par de punteros posibles:

+0

Ya probé esos ejemplos, y funcionan muy bien con gmail, pero no puedo hacer que funcionen con SMTP regular a través de SSL en mis cuentas de ISP. –

0

Si usted tiene que utilizar SSL en lugar de TLS se puede neto mono-parche :: SMTP como esto:

require "openssl" 
require "net/smtp" 

Net::SMTP.class_eval do 

    def self.start(address, port = nil, 
        helo = 'localhost.localdomain', 
        user = nil, secret = nil, authtype = nil, use_tls = false, 
        use_ssl = false, &block) # :yield: smtp 
    new(address, port).start(helo, user, secret, authtype, use_tls, use_ssl, &block) 
    end 

    def start(helo = 'localhost.localdomain', 
      user = nil, secret = nil, authtype = nil, use_tls = false, use_ssl = false) # :yield: smtp 
    start_method = use_tls ? :do_tls_start : use_ssl ? :do_ssl_start : :do_start 
    if block_given? 
     begin 
     send start_method, helo, user, secret, authtype 
     return yield(self) 
     ensure 
     do_finish 
     end 
    else 
     send start_method, helo, user, secret, authtype 
     return self 
    end 
    end 

    private 

    def do_tls_start(helodomain, user, secret, authtype) 
    raise IOError, 'SMTP session already started' if @started 

    if VERSION == '1.8.6' 
     check_auth_args user, secret, authtype if user or secret 
    elsif VERSION == '1.8.7' 
     check_auth_args user, secret 
    end 

    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) 

    raise 'openssl library not installed' unless defined?(OpenSSL) 
    starttls 
    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) 

    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_ssl_start(helodomain, user, secret, authtype) 
    raise IOError, 'SMTP session already started' if @started 

    if VERSION == '1.8.6' 
     check_auth_args user, secret, authtype if user or secret 
    elsif VERSION == '1.8.7' 
     check_auth_args user, secret 
    end 

    sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) } 
    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 

    check_response(critical { recv_response() }) 
    do_helo(helodomain) 

    do_helo(helodomain) 

    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') 
    end 

    def quit 
    begin 
     getok('QUIT') 
    rescue EOFError, OpenSSL::SSL::SSLError 
    end 
    end 
end 

Ver http://github.com/collectiveidea/action_mailer_optional_tls/blob/master/lib/smtp_tls.rb

9

puedo solucionar este problema con esta configuración a continuación:

config.action_mailer.perform_deliveries = true 
config.action_mailer.raise_delivery_errors = false 
config.action_mailer.delivery_method = :smtp 
config.action_mailer.smtp_settings = { 
    :address    => 'mail.domain.com', 
    :port     => '465', 
    :domain    => 'yourdomain.com', 
    :user_name   => '[email protected]', 
    :password    => 'yourpassword', 
    :authentication  => :login, 
    :ssl     => true, 
    :openssl_verify_mode => 'none' #Use this because ssl is activated but we have no certificate installed. So clients need to confirm to use the untrusted url. 
} 

Funciona muy bien para mí.

+1

Estaba peleando con notificaciones de gitlab/mail -: ssl => verdadero funcionó para mí. –

+0

el documento README.md (https://github.com/mikel/mail#getting-emails-from-a-pop-server) para Mail Gem es realmente antiguo, por lo que ': enable_ssl => true' ya no funciona, en su lugar use ': ssl => true', qué dolor 〒_〒 – DiveInto

Cuestiones relacionadas