Rails 3:序列化后无法传递 Mail::Message

发布于 2024-11-06 15:26:39 字数 3637 浏览 0 评论 0原文

我想序列化一个 Mail::Message 对象,以便将其存储在数据库中并稍后发送。 我的第一次尝试是使用 ActiveRecord 的内置 serialize 方法,如下所示:

class Email < ActiveRecord::Base
  serialize :mail

  # ...

end

不幸的是,尝试保存记录导致了异常

> foo.save
ArgumentError: wrong number of arguments (1 for 0)
from /home/akrueger/.rvm/gems/ruby-1.9.2-p180/gems/mail-2.2.19/lib/mail/message.rb:1714:in `to_yaml'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:55:in `accept'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:36:in `<<'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych.rb:165:in `dump'
from /home/akrueger/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.7/lib/active_record/base.rb:1720:in `block in arel_attributes_values'
    (...)

所以我只是使用 to_yaml 创建了一个自定义访问器Mail::Message 的 code> / from_yaml 函数。但现在我遇到了这种奇怪的行为:

mail_original = UserMailer.user_welcome_email(@user)
=> #<Mail::Message:93415800, Multipart: false, Headers: <From: (...) >, <To: (...) >, <Subject: (...) >, <Mime-Version: 1.0>, <Content-Type: text/plain>>
mail_clone = Mail::Message.from_yaml mail_original.to_yaml
=> #<Mail::Message:92940980, Multipart: false, Headers: <Date: (...) >, <From: (...) >, <To: (...) >, <Message-ID: (...) >, <Subject: (...) >, <Mime-Version: 1.0>, <Content-Type: text/plain>, <Content-Transfer-Encoding: quoted-printable>>
mail_original.deliver
=> #<Mail::Message:93415800, Multipart: false, Headers: <Date: (...) >, <From: (...) >, <To: (...) >, <Message-ID: (...) >, <Subject: (...) >, <Mime-Version: 1.0>, <Content-Type: text/plain>, <Content-Transfer-Encoding: quoted-printable>>
mail_clone.deliver
Errno::ECONNREFUSED: Connection refused - connect(2)
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:546:in `initialize'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:546:in `open'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:546:in `tcp_socket'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:555:in `block in do_start'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/timeout.rb:57:in `timeout'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/timeout.rb:87:in `timeout'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:555:in `do_start'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:525:in `start'
from /home/akrueger/.rvm/gems/ruby-1.9.2-p180/gems/mail-2.2.19/lib/mail/network/delivery_methods/smtp.rb:128:in `deliver!'
from /home/akrueger/.rvm/gems/ruby-1.9.2-p180/gems/mail-2.2.19/lib/mail/message.rb:1989:in `do_delivery'
from /home/akrueger/.rvm/gems/ruby-1.9.2-p180/gems/mail-2.2.19/lib/mail/message.rb:232:in `deliver'
from (irb):68
from /home/akrueger/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands/console.rb:44:in `start'
from /home/akrueger/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands/console.rb:8:in `start'
from /home/akrueger/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

知道如何解决这个问题吗?为什么将对象序列化为 yaml 会使 Message 对象无法投递?是否有另一种方法可以正确序列化 Message 对象以便稍后发送?

I want to serialize a Mail::Message object in order to store it in the database and send it later.
My first attempt was to use the built-in serialize method of ActiveRecord like this:

class Email < ActiveRecord::Base
  serialize :mail

  # ...

end

Unfortunately, trying to save the record resulted in an exception

> foo.save
ArgumentError: wrong number of arguments (1 for 0)
from /home/akrueger/.rvm/gems/ruby-1.9.2-p180/gems/mail-2.2.19/lib/mail/message.rb:1714:in `to_yaml'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:55:in `accept'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:36:in `<<'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych.rb:165:in `dump'
from /home/akrueger/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.7/lib/active_record/base.rb:1720:in `block in arel_attributes_values'
    (...)

So I simply created a custom accessor using the to_yaml / from_yaml functions of Mail::Message. But now I experience this strange behaviour:

mail_original = UserMailer.user_welcome_email(@user)
=> #<Mail::Message:93415800, Multipart: false, Headers: <From: (...) >, <To: (...) >, <Subject: (...) >, <Mime-Version: 1.0>, <Content-Type: text/plain>>
mail_clone = Mail::Message.from_yaml mail_original.to_yaml
=> #<Mail::Message:92940980, Multipart: false, Headers: <Date: (...) >, <From: (...) >, <To: (...) >, <Message-ID: (...) >, <Subject: (...) >, <Mime-Version: 1.0>, <Content-Type: text/plain>, <Content-Transfer-Encoding: quoted-printable>>
mail_original.deliver
=> #<Mail::Message:93415800, Multipart: false, Headers: <Date: (...) >, <From: (...) >, <To: (...) >, <Message-ID: (...) >, <Subject: (...) >, <Mime-Version: 1.0>, <Content-Type: text/plain>, <Content-Transfer-Encoding: quoted-printable>>
mail_clone.deliver
Errno::ECONNREFUSED: Connection refused - connect(2)
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:546:in `initialize'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:546:in `open'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:546:in `tcp_socket'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:555:in `block in do_start'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/timeout.rb:57:in `timeout'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/timeout.rb:87:in `timeout'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:555:in `do_start'
from /home/akrueger/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:525:in `start'
from /home/akrueger/.rvm/gems/ruby-1.9.2-p180/gems/mail-2.2.19/lib/mail/network/delivery_methods/smtp.rb:128:in `deliver!'
from /home/akrueger/.rvm/gems/ruby-1.9.2-p180/gems/mail-2.2.19/lib/mail/message.rb:1989:in `do_delivery'
from /home/akrueger/.rvm/gems/ruby-1.9.2-p180/gems/mail-2.2.19/lib/mail/message.rb:232:in `deliver'
from (irb):68
from /home/akrueger/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands/console.rb:44:in `start'
from /home/akrueger/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands/console.rb:8:in `start'
from /home/akrueger/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

Any idea on how to solve this problem? Why does serializing the object to yaml make the Message object undeliverable? Is there maybe another method to properly serialize the Message object to send it later?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迎风吟唱 2024-11-13 15:26:39

我意识到 mail_clone 对象没有正确的传递设置,这导致 deliver 调用失败。浏览 Rails 源代码,我找到了以下解决方案:

mail_clone = Mail::Message.from_yaml mail_original.to_yaml
ActionMailer::Base.wrap_delivery_behavior mail_clone
mail_clone.deliver

所以这可能是序列化 Mail::Message 对象的一种方法:

class Email < ActiveRecord::Base
  def mail
    @mail_cached || begin
      m = Mail::Message.from_yaml(self.read_attribute :mail)
      ActionMailer::Base.wrap_delivery_behavior m
      m
    rescue
      nil
    end
  end

  def mail=(val)
    @mail_cached = val
    write_attribute :mail, val.to_yaml
  end

  # ...
end

I realized the mail_clone object did not have proper delivery settings, which caused the failure of the deliver call. Browsing through the Rails sourcecode, I found the following solution:

mail_clone = Mail::Message.from_yaml mail_original.to_yaml
ActionMailer::Base.wrap_delivery_behavior mail_clone
mail_clone.deliver

So this might be a way to serialize the Mail::Message object:

class Email < ActiveRecord::Base
  def mail
    @mail_cached || begin
      m = Mail::Message.from_yaml(self.read_attribute :mail)
      ActionMailer::Base.wrap_delivery_behavior m
      m
    rescue
      nil
    end
  end

  def mail=(val)
    @mail_cached = val
    write_attribute :mail, val.to_yaml
  end

  # ...
end
奢望 2024-11-13 15:26:39

指定类名有帮助吗?

serialize :mail, 'Mail::Message' 

Does specifying the class name help?

serialize :mail, 'Mail::Message' 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文