如何让邮件不再重复

时间:2012-07-27 21:37:04

标签: ruby-on-rails-3 actionmailer

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

该模型只是通过一系列收件人来实现......

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接收所有电子邮件,终端说他们已被发送。

然而,在第一个&lt;&lt;&lt;&lt;&lt;&lt;&lt; @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

(这将是发送的第五条消息,每发送一封电子邮件会逐渐变得更糟。)

这是我不明白的,基于我对Mail和方法调用的理解,发送的每条消息都应该是它们自己的唯一对象,并且根本不应该相互交互。然而,显然是。在text和html中使用+ =和&lt;&lt ;,会发生这种情况。 WTF?

任何想法或帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

您实际上是使用<<修改实例变量。如果您使用+,我认为它应该有用。