2010-11-30 9 views
5

を達成するためにどのように私は正常にこのスクリプトを使ってポート彼らのポート25(非セキュア)を使用してリモートサーバーに電子メールを送信されました:私は今行う必要がある何ルビーメールは、SSLメール

require 'rubygems' 
require 'mail' 

options = { :address    => "mail.domain.com", 
      :port     => 25, 
      :domain    => 'mail.domain.com', 
      :user_name   => '[email protected]', 
      :password    => 'topsecret', 
      :authentication  => 'login', 
      :enable_starttls_auto => true } 
Mail.defaults do 
    delivery_method :smtp, options 
end 

mail = Mail.new do 
     from '[email protected]' 
     to '[email protected]' 
    subject 'This is a test email' 
     body File.read('body.txt') 
end 

puts mail.to_s 
mail.deliver! 

が使用され、その

/usr/local/rvm/rubies/ruby-1.8.7-p249/lib/ruby/1.8/timeout.rb:60:in `rbuf_fill': execution expired (Timeout::Error) 
     from /usr/local/rvm/rubies/ruby-1.8.7-p249/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill' 
     from /usr/local/rvm/rubies/ruby-1.8.7-p249/lib/ruby/1.8/net/protocol.rb:116:in `readuntil' 
     from /usr/local/rvm/rubies/ruby-1.8.7-p249/lib/ruby/1.8/net/protocol.rb:126:in `readline' 
     from /usr/local/rvm/rubies/ruby-1.8.7-p249/lib/ruby/1.8/net/smtp.rb:911:in `recv_response' 
     from /usr/local/rvm/rubies/ruby-1.8.7-p249/lib/ruby/1.8/net/smtp.rb:554:in `do_start' 
     from /usr/local/rvm/rubies/ruby-1.8.7-p249/lib/ruby/1.8/net/smtp.rb:921:in `critical' 
     from /usr/local/rvm/rubies/ruby-1.8.7-p249/lib/ruby/1.8/net/smtp.rb:554:in `do_start' 
     from /usr/local/rvm/rubies/ruby-1.8.7-p249/lib/ruby/1.8/net/smtp.rb:525:in `start' 
     from /usr/local/rvm/gems/ruby-1.8.7-p249/gems/mail-2.2.10/lib/mail/network/delivery_methods/smtp.rb:127:in `deliver!' 
     from /usr/local/rvm/gems/ruby-1.8.7-p249/gems/mail-2.2.10/lib/mail/message.rb:243:in `deliver!' 
     from testmail.rb:30 

私はそれもSSL認証プロセスを開始することができないため、これがあると思う:私はそれをしようとするとSSLポート466は、私はそれは約2分間一時停止し、これまで咳、メッセージの詳細を通常の出力を得ます。どうすればいいのですか?

答えて

4

まあ、ネットワーク/ delivery_methods/smtp.rbを読むと、直接SSLをサポートしていないように見えます。 TLSは、接続がプレーンテキストを開始し、starttlsコマンドでSSLに切り替わるため、同じではありません。ポート587で始動を使用できますか?

コメントを引きます。

あなたは猿のパッチはNet :: SMTPそれを行うためにできることを示唆している

How to send mail with ruby over smtp with ssl (not with rails, no TLS for gmail)

を参照してください。..

okがちょっと問題を発見し、それを中心にパッチを適用することができますが、これまでのところ、このソリューション不潔な..ですが、それは作業を行います:)

#!/usr/bin/env ruby 

require 'rubygems' 
require "openssl" 
require "net/smtp" 
require "mail" 

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 = true, &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 = true) # :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 

    check_auth_args user, 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) 

    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 

    check_auth_args user, secret 

    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 

options = { 
    :address    => "mail.domain.net", 
    :port     => 466, 
    :domain    => 'mail.domain.net', 
    :user_name   => '[email protected]', 
    :password    => 'Secret!', 
    :authentication  => 'login', 
    :use_ssl => true } 

Mail.defaults do 
    delivery_method :smtp, options 
end 

mail = Mail.new do 
    from '[email protected]' 
    to '[email protected]' 
    subject 'This is a test email' 
    body File.read('body.txt') 
end 

puts mail.to_s 
mail.deliver! 

ORIGのモンキーパッチでUSE_SSLはそれを作るていない何らかの理由で、とでは、VEというカップルNet :: SMTPではRSIONが未定義です。だから私はそれを変更し、use_sslをtrueに強制し、電子メールを送ることができました。

+0

私たちは466のポート割り当てを制御しておらず、ポート587は利用できません。 –

+0

Net :: SMTPは、メールが使用するボックスからすぐにSSLをサポートしているようです。ここを見てください。 http://stackoverflow.com/questions/708858/how-to-send-mail-with-ruby-over-smtp-with-ssl-not-with-rails-no-tls-for-gmailあなたは猿のように見えますパッチNet :: SMTPは動作します.. – Doon

+0

私はそれを試みましたが、残念なことにまったく同じ結果を生成します。 –

0

ポート465を使用しましたか?それは466ではなく、標準のフォールバックポートです。間違ったポートに接続するタイミングが間違っている可能性があります。

+0

いいえ、ポートは当社のサービスプロバイダによって割り当てられました。誰か他の人が実際にそれを試してみたが、役に立たなかった。 –

+0

「telnet mail.myserver.com 466」とはどうなりますか?あなたはまったく接続しますか? –

+0

番号。 465も試しました。 「mail.safetysend.comに接続しています...」と表示されます。 –

関連する問題