4

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

This is my code:

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

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?

Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
bigpotato
  • 26,262
  • 56
  • 178
  • 334

3 Answers3

7

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

What is This Library NOT?¶ ↑

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)

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

and now it works!

bigpotato
  • 26,262
  • 56
  • 178
  • 334
1

I know this mostly useless now but, I tried sending email using Net::SMTP and was able to set the subject. maybe you could have tried

Subject: 'test message' instead of Subject: test message

Amol
  • 318
  • 2
  • 6
1

I actually managed to send a mail with NET::SMTP with a subject, I think that the indentation is important (example taken from 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

The Subject will be correct. If you print the variable "message" you'll get this output-

"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"

While this code, with spaces in front of "Subject" -

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

will print -

"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"

And then the subject won't work.

deez
  • 1,472
  • 3
  • 18
  • 28