4

Ruby アプリがあり、ドキュメントhttp://ruby-doc.org/stdlib-2.0/libdoc/net/smtp/rdoc/Net/SMTP.htmlにあるこの形式のメールを送信しています。

Net::SMTP.start('your.smtp.server', 25) do |smtp|
    smtp.send_message msgstr, 'from@address', 'to@address'
end

これは私のコードです:

def send_notification(exception)

    msgstr = <<-END_OF_MESSAGE
        From: Exchange Errors <exchangeerrors@5112.mysite.com>
        To: Edmund Mai <emai@mysite.com>
        Subject: test message
        Date: Sat, 23 Jun 2001 16:26:43 +0900
        Message-Id: <unique.message.id.string@mysite.com>

        This is a test message.
    END_OF_MESSAGE


    Net::SMTP.start('localhost', 25) do |smtp|
        smtp.send_message(msgstr, "exchangeerrors@5112.mysite.com", "emai@mysite.com")
    end
end

ただし、送信されるメールには件名がありません。msgstrがそのままメール本文になります。メールの件名を指定する方法に関するドキュメントのどこにもありません。誰か知っていますか?

4

3 に答える 3

7

ドキュメントを見たところ、Net::SMTP はこれをサポートしていないようです。ドキュメントでは、次のように述べています。

このライブラリとは何ですか?¶ ↑</p>

このライブラリは、インターネット メールを作成する機能を提供しません。自分で作成する必要があります。より良いメール サポートが必要な場合は、RubyMail または TMail を試してください。どちらのライブラリも RAA から入手できます。(www.ruby-lang.org/en/raa.html)

そこで、実際に Net::SMTP を使用する MailFactory gem ( http://mailfactory.rubyforge.org/ ) を調べました。

    require 'net/smtp'
    require 'rubygems'
    require 'mailfactory'

    mail = MailFactory.new()
    mail.to = "test@test.com"
    mail.from = "sender@sender.com"
    mail.subject = "Here are some files for you!"
    mail.text = "This is what people with plain text mail readers will see"
    mail.html = "A little something <b>special</b> for people with HTML readers"
    mail.attach("/etc/fstab")
    mail.attach("/some/other/file")

    Net::SMTP.start('smtp1.testmailer.com', 25, 'mail.from.domain', fromaddress, password, :cram_md5) { |smtp|
      mail.to = toaddress
      smtp.send_message(mail.to_s(), fromaddress, toaddress)
    }

そして今それは動作します!

于 2013-10-02T18:47:26.293 に答える
1

これはほとんど役に立たないことはわかっていますが、Net::SMTP を使用してメールを送信しようとしたところ、件名を設定できました。多分あなたは試すことができたでしょう

Subject: 'test message'それ以外のSubject: test message

于 2014-07-15T23:02:43.317 に答える
1

実際に NET::SMTP で件名付きのメールを送信できました。インデントが重要だと思います ( http://www.tutorialspoint.com/ruby/ruby_sending_email.htmからの例) -

message = <<MESSAGE_END
From: Private Person <me@fromdomain.com>
To: A Test User <test@todomain.com>
Subject: SMTP e-mail test

This is a test e-mail message.
MESSAGE_END
Net::SMTP.start('localhost') do |smtp|
  smtp.send_message message, 'me@fromdomain.com', 
                             'test@todomain.com'
end

件名は正しいでしょう。変数「メッセージ」を出力すると、次の出力が得られます-

"From: Private Person <me@fromdomain.com>\nTo: A Test User <test@todomain.com>\nSubject: SMTP e-mail test\n\nThis is a test e-mail message.\n"

このコードでは、「件名」の前にスペースがあります -

message = <<MESSAGE_END
From: Private Person <me@fromdomain.com>
To: A Test User <test@todomain.com>
   Subject: SMTP e-mail test

This is a test e-mail message.
MESSAGE_END
Net::SMTP.start('localhost') do |smtp|
  smtp.send_message message, 'me@fromdomain.com', 
                             'test@todomain.com'
end

印刷されます -

"From: Private Person <me@fromdomain.com>\nTo: A Test User <test@todomain.com>\n   Subject: SMTP e-mail test\n\nThis is a test e-mail message.\n"

それでは、話題が尽きません。

于 2015-02-02T15:53:21.067 に答える