AnApprentice
AnApprentice

Reputation: 110960

When sending one email, the log shows the email twice? Though it's sending only once?

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

Upvotes: 0

Views: 1678

Answers (1)

Simon
Simon

Reputation: 1736

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.

Upvotes: 9

Related Questions