0

因此,我设法创建了一些我不知道如何调试的有趣行为。我有一个由模型和表格支持的邮件。当用户创建消息时,一个方法会生成一组联系人来发送电子邮件。既然已经在控制台中进行了测试,我们将在那里进行测试。

该模型只是遍历一组接收者......

class ContactMessage < ActiveRecord::Base
  ...
  def send_message(user)
    self.recipients.each do |rec|
      unless rec.include?("@")
        contact = Contact.find(rec)
        to = "\"#{contact.first_name} #{contact.last_name}\" <#{contact.email}>"
      else
        to = rec
        contact = user.contact.new('email' => rec)
      end
     ContactMail.direct_mail(user, self, to, contact).deliver
    end
  end

end

然后它应该对 ContactMail.direct_mail 方法进行 n 次新调用。

class ContactMail < ActionMailer::Base
  helper :mail
  def direct_mail(user, contact_message, to, contact)
    @user = user
    @contact = contact
    @contact_message = contact_message
    @theme = @contact_message.theme
    mail(:to => to, :subject => contact_message.subject, :from => "no-reply" << @user.website.domain, :reply_to => @user.email)
  end

  ...
end

mail() 方法使用提供的@instance 变量呈现视图。

<%= @user.website.title %>
<%= @user.website.motto %> 
============================================================
<%= @contact_message.message.html_safe.gsub(/<\/?[^><]*>/i, "") %>
============================================================
This message is from <%= @user.first_name << " " << @user.last_name << " of " << @user.business%>
Please reply to <%= @user.email %>
<%= @user.telephone %>
<%= @user.address_l1%>
<%= @user.address_l2 unless @user.address_l2.blank?%>
<%= @user.city << ", " << @user.state << " " << @user.zip %>
<%= @user.website.domain %>

一切都很好,我正在使用 MailCatcher 接收所有的电子邮件,并且终端说它们已发送。

但是,在第一个 << @instance 之后发送的每封邮件都在不断堆积!它产生类似的东西

Healthy Living
Where massage makes health. 
============================================================
asdfasdfasdfasd
============================================================
This message is from Adam Fluke of Healthy Living LLC Fluke of Healthy Living LLC Fluke of Healthy Living LLC Fluke of Healthy Living LLC Fluke of Healthy Living LLC Fluke of Healthy Living LLC Fluke of Healthy Living LLC Fluke of Healthy Living LLC Fluke of Healthy Living LLC Fluke of Healthy Living LLC
Please reply to fluke.a@gmail.com
504-638-2222
1822 Moss St
Apt E
New Orleans, AL 70119, AL 70119, AL 70119, AL 70119, AL 70119, AL 70119, AL 70119, AL 70119, AL 70119, AL 70119, AL 70119, AL 70119, AL 70119, AL 70119, AL 70119, AL 70119, AL 70119, AL 70119, AL 70119, AL 70119
healthyliving.org

(this would be the fifth message sent, it gets progressively worse with each email sent.)

This is what I don't understand, based on my understanding of Mail and method calls, each message sent should be their own unique object and should not be interacting with eachother at all. Yet, the clearly are. This happens with += and <<, in text and html. WTF?

Any thoughts or help appreciated.

4

1 回答 1

0

You are actually modifying your instance variables by using <<. If you use + I think it should work.

于 2012-07-30T06:45:52.723 回答