# Send mail only when a condition satisfy

**URL:** <https://www.ruby-forum.com/t/send-mail-only-when-a-condition-satisfy/152976>\
**Category:** Rails\
**Created:** [November 10, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/send-mail-only-when-a-condition-satisfy/152976 "2008-11-10T19:50:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dat\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@Dat\_C](https://www.ruby-forum.com/u/Dat_C)\
**Post date:** [November 10, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/send-mail-only-when-a-condition-satisfy/152976/1 "2008-11-10T19:50:13Z")

</div>

I would like to send a digest of what have been changed in a day to  
some admins. However, some day there won’t be any change. How do I not  
send emails on such cases?

Currently, my mailer is called Postoffice. I call  
Postoffice.deliver\_daily\_digest every night by a cron job.

Inside of daily\_digest method, I have queries to figure out what have  
been changed. I don’t want to repeat these queries in my rake task.  
Any suggestions would be appreciated.

---

<div class="post-metadata">

**Author:** ![Dat\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@Dat\_C](https://www.ruby-forum.com/u/Dat_C)\
**Post date:** [November 10, 2008, 9:33pm UTC](https://www.ruby-forum.com/t/send-mail-only-when-a-condition-satisfy/152976/2 "2008-11-10T21:33:21Z")

</div>

On 10 Nov 2008, at 16:32, Dat C. wrote:

> Any suggestions would be appreciated.

I would extract those queries from the mailer into some other class  
(eg DailyReporter).  
The mailer is then just something dumb which inserts data passed to it  
in various parts of the template

Usage might look like

report = DailyReporter.run  
if report.anything\_happened?  
Postoffice.deliver\_daily\_digest(report)  
end

Fred
