问题
I use Ruby net/smtp
require 'net/smtp'
I send mails using the following method:
Net::SMTP.start(SMTP_SERVER) do |smtp|
smtp.send_message(message,"mailman-ruby@example.de",reciepent)
end
I want to add a subject to the mails i send but couldn't find one in the documentation.
What I tried so far is putting the subject into the message like this:
<<END_OF_MESSAGE
Subject: test message
. . . rest of the message
END_OF_MESSAGE
Unfortunally that doesnt do the trick!
Anyone knows how to set subject using 'smtp/net'?
回答1:
net/smtp
is a very low-level library. It expects the message
to be an email message, including all the headers.
From the documentation
msgstr = <<END_OF_MESSAGE
From: Your Name <your@mail.address>
To: Destination Address <someone@example.com>
Subject: test message
Date: Sat, 23 Jun 2001 16:26:43 +0900
Message-Id: <unique.message.id.string@example.com>
This is a test message.
END_OF_MESSAGE
require 'net/smtp'
Net::SMTP.start('your.smtp.server', 25) do |smtp|
smtp.send_message msgstr,
'your@mail.address',
'his_address@example.com'
end
In your case, make sure to add a new line between the Headers and the body. Change
<<END_OF_MESSAGE
Subject: test message
. . . rest of the message
END_OF_MESSAGE
to
<<END_OF_MESSAGE
Subject: test message
. . . rest of the message
END_OF_MESSAGE
Instead of using net/smtp
, I highly encourage you to use mail. mail
uses a more high level interface for sending email messages without loosing the control of the message.
If you don't really care about advanced usage, you can also switch to pony.
回答2:
The responsibility of the subject is not that of the SMTP protocol. It is held in one of the SMTP commands, which is to say DATA
and as such, is not relevant in responsibility to that library.
However, the DATA
will contain either proper (specification indicated) headers, or even some headers that are not specified, but not disallowed. PGP
According to RFC 5321 (and as expected) "Server SMTP systems SHOULD NOT reject messages based on perceived defects in the RFC 822 or MIME (RFC 2045 [21]) message header section or message body.".
So, your answer is going to be: "You don't set the subject by using that." But you would set the DATA with that information in it.
And the documentation that you linked to has an example of doing exactly that:
msgstr = <<END_OF_MESSAGE
From: Your Name <your@mail.address>
To: Destination Address <someone@example.com>
Subject: test message
Date: Sat, 23 Jun 2001 16:26:43 +0900
Message-Id: <unique.message.id.string@example.com>
This is a test message.
END_OF_MESSAGE
require 'net/smtp'
Net::SMTP.start('your.smtp.server', 25) do |smtp|
smtp.send_message msgstr,
'your@mail.address',
'his_address@example.com'
end
Documentation not linked here, as you have already provided the link.
来源:https://stackoverflow.com/questions/20632936/email-subject-not-correctly-assigned-with-ruby-net-smtp