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

发布于 2024-12-07 14:51:51 字数 2494 浏览 0 评论 0原文

在我的应用程序中,DJ 发送了一封电子邮件,但由于某种原因,我在日志文件中看到该电子邮件两次,尽管它只发送一次?我在日志中确认 DJ 仅运行一次并且 user_mailer 仅运行一次。那么为什么我会在日志文件中看到该电子邮件两次? Rails 在做什么?

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

Sent mail to [email protected] (1097ms)
Date: Fri, 30 Sep 2011 13:34:56 -0700
From: "roomxcom" <no-reply@roomxcom>
To: [email protected]
Message-ID: <[email protected]>
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 [email protected] (3510ms)
Date: Fri, 30 Sep 2011 13:34:56 -0700
From: "roomxcom" <no-reply@roomxcom>
To: [email protected]
Message-ID: <[email protected]>
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 <[email protected]>",
      :to => user_recipient.email,
      :subject => "[#{@room.title}] #{@item.title}"
    ).deliver

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

in my app, DJ fires off a an email but for some reason I see the email in the log file twice though it is sent only once? I confirmed in the logs that DJ runs only once and user_mailer runs only once. So why do I see the email twice in the log file? What is Rails doing?

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

Sent mail to [email protected] (1097ms)
Date: Fri, 30 Sep 2011 13:34:56 -0700
From: "roomxcom" <no-reply@roomxcom>
To: [email protected]
Message-ID: <[email protected]>
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 [email protected] (3510ms)
Date: Fri, 30 Sep 2011 13:34:56 -0700
From: "roomxcom" <no-reply@roomxcom>
To: [email protected]
Message-ID: <[email protected]>
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

Delayed Job:

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

User Mailer:

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

Any idea what' going on and why rails is showing the email in the log twice? Thanks

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

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

发布评论

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

评论(1

跨年 2024-12-14 14:51:51

编辑:现在是白天,我喝了一些咖啡,这里有一个更清晰的解释...

这一行将消息添加到队列中:

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

因此 Delayed_job 然后调用 UserMailer< 中的 room_notification 方法/code> 发送邮件:

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

这本身就足以发送邮件,但在 mail 方法的末尾还有一个多余的 .deliver ,因此它正在发送两次。

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

edit: Here's a clearer explanation now that it's daytime and I've had some coffee...

This line is adding the message to the queue:

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

So Delayed_job then calls the room_notification method in UserMailer to send the mail:

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

This would be enough in itself to send the mail but you also have a redundant .deliver on the end of the mail method so it's sending it twice.

TL;DR Remove .deliver from mail(...) in the room_notification method and everything should be fine.

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