Ruby和使用Net :: SMTP发送电子邮件:如何指定电子邮件主题? [英] Ruby and sending emails with Net::SMTP: How to specify email subject?

查看:77
本文介绍了Ruby和使用Net :: SMTP发送电子邮件:如何指定电子邮件主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ruby应用程序,我正在使用文档

I have a ruby app and I am sending emails with this format found in the documentation 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 成为电子邮件的正文.我没有在文档中的任何地方看到如何指定邮件主题.有人知道吗?

However, the emails being sent have no subjects in them. The msgstr just becomes the body of the email. I don't see anywhere in the documentation on how to specify a mail subject. Does anyone know?

推荐答案

所以我看了看文档,看起来Net :: SMTP不支持此文件.在文档中说:

So I looked at the documentation and it looks like Net::SMTP doesn't support this. In the documentation it says this:

这个库不是什么?¶↑

此库不提供撰写Internet邮件的功能.您必须自己创建它们.如果您想要更好的邮件支持,请尝试RubyMail或TMail.您可以从RAA获得这两个库.(www.ruby-lang.org/en/raa.html)

This library does NOT provide functions to compose internet mails. You must create them by yourself. If you want better mail support, try RubyMail or TMail. You can get both libraries from RAA. (www.ruby-lang.org/en/raa.html)

因此,我研究了MailFactory gem( http://mailfactory.rubyforge.org/),实际上使用Net :: SMTP:

So I looked into the MailFactory gem (http://mailfactory.rubyforge.org/), which uses Net::SMTP actually:

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

现在可以使用了!

这篇关于Ruby和使用Net :: SMTP发送电子邮件:如何指定电子邮件主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆