【问题标题】:Ruby and sending emails with Net::SMTP: How to specify email subject?Ruby 和使用 Net::SMTP 发送电子邮件:如何指定电子邮件主题?
【发布时间】:2013-10-09 05:57:37
【问题描述】:

我有一个 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 只是成为电子邮件的正文。我在文档中没有看到有关如何指定邮件主题的任何地方。有人知道吗?

【问题讨论】:

    标签: ruby email


    【解决方案1】:

    所以我查看了文档,看起来 Net::SMTP 不支持这一点。在文档中它是这样说的:

    这个库不是什么?¶ ↑

    此库不提供撰写互联网邮件的功能。您必须自己创建它们。如果您想要更好的邮件支持,请尝试 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)
        }
    

    现在它可以工作了!

    【讨论】:

      【解决方案2】:

      我现在知道这几乎没有用,但是我尝试使用 Net::SMTP 发送电子邮件并且能够设置主题。也许你可以尝试

      Subject: 'test message' 而不是Subject: test message

      【讨论】:

        【解决方案3】:

        我实际上设法发送带有主题的 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"
        

        然后主题就不行了。

        【讨论】:

          猜你喜欢
          • 2016-04-17
          • 1970-01-01
          • 2013-02-03
          • 1970-01-01
          • 2011-07-30
          • 2011-02-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多