gpt4 book ai didi

ruby - 电子邮件主题未正确分配给 Ruby net/smtp

转载 作者:太空宇宙 更新时间:2023-11-03 17:23:50 24 4
gpt4 key购买 nike

我使用 Ruby net/smtp

require 'net/smtp'

我使用以下方法发送邮件:

Net::SMTP.start(SMTP_SERVER) do |smtp|
smtp.send_message(message,"mailman-ruby@example.de",reciepent)
end

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

到目前为止我尝试的是将主题放入消息中,如下所示:

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

不幸的是,这并不能解决问题!

有人知道如何使用“smtp/net”设置主题吗?

最佳答案

net/smtp 是一个非常底层的库。它期望 message 是一封电子邮件,包括所有 header 。

来自文档

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

在您的情况下,请确保在标题和正文之间添加一个新行。变化

<<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,而是使用 mail . mail 使用更高级别的接口(interface)来发送电子邮件消息而不会失去对消息的控制。

如果你不太关心高级用法,你也可以切换到pony .

关于ruby - 电子邮件主题未正确分配给 Ruby net/smtp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20632936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com