发送一封电子邮件时,日志会显示两次电子邮件?虽然它只发送一次?

时间:2011-09-30 20:46:20

标签: ruby-on-rails ruby-on-rails-3 actionmailer

在我的应用程序中,DJ发了一封电子邮件,但出于某种原因,我在日志文件中看到了两次电子邮件,虽然只发送了一次?我在日志中确认DJ只运行一次而user_mailer只运行一次。那么为什么我会在日志文件中看到两次电子邮件? Rails在做什么?

Rendered user_mailer/room_notification.text.erb (0.9ms)

Sent mail to rachela.xxxxxxx@gmail.com (1097ms)
Date: Fri, 30 Sep 2011 13:34:56 -0700
From: "roomxcom" <no-reply@roomxcom>
To: rachela.xxxxxxx@gmail.com
Message-ID: <123@-MacBook-Pro.local.mail>
Subject: [Email Testing] Test 12
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

AAAAA Did something:

Test 12

http://localhost:3000/c19

Sent mail to rachela.xxxxxxx@gmail.com (3510ms)
Date: Fri, 30 Sep 2011 13:34:56 -0700
From: "roomxcom" <no-reply@roomxcom>
To: rachela.xxxxxxx@gmail.com
Message-ID: <123@-MacBook-Pro.local.mail>
Subject: [Email Testing] Test 12
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

AAAAA Did something:

Test 12

http://localhost:3000/c19
  SQL (0.1ms)  BEGIN
  AREL (0.3ms)  DELETE FROM "delayed_jobs" WHERE "delayed_jobs"."id" = 25
  SQL (0.4ms)  COMMIT

延迟工作:

UserMailer.delay.room_notification(room, record.user, room_member.user, record.item)

用户邮件程序:

def room_notification(room, user_creator, user_recipient, item)
  ...
mail( :from => "XXXX <no-reply@XXXXXXXXXX.com>",
      :to => user_recipient.email,
      :subject => "[#{@room.title}] #{@item.title}"
    ).deliver

知道发生了什么以及为什么rails会在日志中显示两次电子邮件?感谢

1 个答案:

答案 0 :(得分:9)

编辑:现在有一个更明确的解释,那就是白天我喝咖啡了......

此行正在将消息添加到队列中:

UserMailer.delay.room_notification(room, record.user, room_member.user, record.item)

然后,Delayed_job调用room_notification中的UserMailer方法发送邮件:

def room_notification(room, user_creator, user_recipient, item)
  # ...
  mail(:from => "XXXX <no-reply@XXXXXXXXXX.com>",
       :to => user_recipient.email,
       :subject => "[#{@room.title}] #{@item.title}"
  ).deliver
end

这本身足以发送邮件,但您在.deliver方法的末尾也有一个冗余mail,因此它会发送两次。

TL; DR在.deliver方法中从mail(...)移除room_notification,一切都应该没问题。