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)

所以我查看了 MailFactory gem ( http://mailfactory.rubyforge.org/ ),它实际上使用了 Net::SMTP:

    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 回答