2009-04-02 4 views
8

Alles, was ich will, ist das Senden von E-Mails aus meinen Ruby-Skripten, über SMTP mit SSL.So senden Sie E-Mails mit Ruby über SMTP mit SSL (nicht mit Rails, kein TLS für Google Mail)

Ich finde nur Beispiele dafür, es aus Rails zu tun, oder für Google Mail mit TLS.

Ich fand Leute sprechen über SMTPS Unterstützung mit Ruby 1.8.5, aber die libdoc erwähnt es nicht.

Jeder mit einem Beispiel zum Senden von Mail über SMTP mit SSL, an Port 465?

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

Antwort

0

Sie wahrscheinlich bereits wissen, über die Net::SMTP standard library

In Bezug auf die SSL-Teil, der nicht aus der Box unterstützt zu werden scheint, fand ich ein paar mögliche Zeiger:

+0

Ich habe diese Beispiele bereits ausprobiert und funktioniert gut mit Google Mail, aber ich kann sie nicht mit regulärem SMTP über SSL auf meinen ISP-Konten arbeiten. –

0

Wenn Sie SSL verwenden, anstatt von TLS Sie Affe-Patch-Net :: SMTP wie diese können:

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 

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

9

ich dieses Problem lösen Siehe mit Diese Konfiguration unten:

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. 
} 

Es funktioniert sehr gut für mich.

+1

War mit Gitlab/Mail-Benachrichtigungen kämpfen -: ssl => true hat für mich gearbeitet. –

+0

die README.md doc (https://github.com/mikel/mail#getting-emails-from-a-pop-server) für Mail Gem ist wirklich alt, also ': enable_ssl => true' funktioniert nicht mehr, Verwenden Sie stattdessen ': ssl => true', was für ein Schmerz 〒_〒 – DiveInto

Verwandte Themen