【问题标题】:How to make a mailer not repeat itself如何让邮件不再重复
【发布时间】:2012-07-26 14:45:42
【问题描述】:

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

模型只是遍历接收者数组...

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

然而,在第一个

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和方法调用的理解,发送的每条消息都应该是自己独特的对象,根本不应该相互交互。然而,显然是。这发生在文本和 html 中的 += 和

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

【问题讨论】:

    标签: ruby-on-rails-3 actionmailer


    【解决方案1】:

    您实际上是在使用&lt;&lt; 修改您的实例变量。如果您使用+,我认为它应该可以工作。

    【讨论】:

    • 事情变得很糟糕,所以我删除了 git 分支并重新开始。作为重写的一部分,我改用了 +。
    猜你喜欢
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多