2013-12-17 31 views
1

我使用Ruby网/ SMTP电子邮件主题不正确使用Ruby净分配/ SMTP

require 'net/smtp' 

我发送邮件使用下面的方法邮件:

Net::SMTP.start(SMTP_SERVER) do |smtp| 
    smtp.send_message(message,"[email protected]",reciepent) 
end 

我想一个主题添加到邮件我发送,但在the documentation找不到。

我试过到目前为止是把主题到消息是这样的:

<<END_OF_MESSAGE 
Subject: test message 
. . . rest of the message 
END_OF_MESSAGE 

Unfortunally是没有做的伎俩!

任何人都知道如何使用'smtp/net'设置主题?

+0

这并不能解决问题,但它有什么作用? – vgoff

回答

5

net/smtp是一个非常低级的图书馆。它期望message是一个电子邮件消息,包括所有标题。

从文档

msgstr = <<END_OF_MESSAGE 
From: Your Name <[email protected]> 
To: Destination Address <[email protected]> 
Subject: test message 
Date: Sat, 23 Jun 2001 16:26:43 +0900 
Message-Id: <[email protected]> 

This is a test message. 
END_OF_MESSAGE 

require 'net/smtp' 
Net::SMTP.start('your.smtp.server', 25) do |smtp| 
    smtp.send_message msgstr, 
        '[email protected]', 
        '[email protected]' 
end 

在你的情况,一定要添加页眉和主体之间的新的生产线。更改

<<END_OF_MESSAGE 
Subject: test message 
. . . rest of the message 
END_OF_MESSAGE 

<<END_OF_MESSAGE 
Subject: test message 
. . . rest of the message 
END_OF_MESSAGE 

而不是使用net/smtp的,我强烈建议你使用mailmail使用更高级别的界面来发送电子邮件而不失去对邮件的控制。

如果您不太在意高级用法,您也可以切换到pony

+0

在我得到女孩上学之前应该提交提交:)否则,我可能没有发布我的答案,我知道这个人坐在这里。好的回答+1 – vgoff

1

该主题的责任不是SMTP协议的责任。它被保存在一个SMTP命令中,也就是说DATA,因此与该库的责任无关。

但是,DATA将包含正确的(指定的)标头,或甚至一些没有指定但不被禁止的标头。 PGP

根据RFC 5321(和预期的一样)“服务器SMTP系统不应该基于RFC 822或MIME(RFC 2045 [21])消息头部分或消息体中的感知缺陷拒绝消息。”。

所以,你的答案是:“你不用这个来设置主题。”但是你可以在其中设置DATA。

而且你链接到文件恰好有这样做的一个例子:

msgstr = <<END_OF_MESSAGE 
From: Your Name <[email protected]> 
To: Destination Address <[email protected]> 
Subject: test message 
Date: Sat, 23 Jun 2001 16:26:43 +0900 
Message-Id: <[email protected]> 

This is a test message. 
END_OF_MESSAGE 

require 'net/smtp' 
Net::SMTP.start('your.smtp.server', 25) do |smtp| 
    smtp.send_message msgstr, 
        '[email protected]', 
        '[email protected]' 
end 

文档没有链接在这里,因为你已经提供的链接。