Forum: Ruby on Rails Send mail every day

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Marco D. (Guest)
on 2015-07-06 18:53
(Received via mailing list)
Hi,


I need to implement a notification feature in my project. My user model
has
a daily, weekly and monthly booleans fields. I already have a UserMailer
that sends mail when I create an account, or for password resetting.


What I want is my app to send a mail each day or week or month,
depending
on what the user chose.


So I've installed Whenever gem <https://github.com/javan/whenever>, and
here is my schedule.rb :

every :day, :at => '12pm' do # Use any day of the week or :weekend,
     :weekday rake "email_sender"
end



My rake task :

desc "Send email to users"

task :email_sender => :environment do |_, args|
    User.find_each do |user|
        UserMailer.daily_mail(user).deliver if user.daily == true
    end
end



My UserMailer :

def daily_mail(user)
     @user = user mail to: user.email, subject: "Mail journalier"
end


The thing is, I don't know if I did right, and how can I test this,
since
I'm not in development ?

I did a crontab -l and I get this :

# Begin Whenever generated tasks for: store

0 12 * * * /bin/bash -l -c 'cd /Users/Marco/Documents/TBProject &&
RAILS_ENV=production bundle exec rake email_sender --silent'


# End Whenever generated tasks for: store



# Begin Whenever generated tasks for:
/Users/Marco/Documents/TBProject/config/schedule.rb

0 12 * * * /bin/bash -l -c 'cd /Users/Marco/Documents/TBProject &&
RAILS_ENV=production bundle exec rake email_sender --silent'


# End Whenever generated tasks for:
/Users/Marco/Documents/TBProject/config/schedule.rb


If you could help me please
Marco D. (Guest)
on 2015-07-06 22:48
(Received via mailing list)
Small update

I tried to run the command rake email_sender in my terminal, and I got
this
:

[1m[36mUser Load (0.9ms)[0m  [1mSELECT  "users".* FROM "users"  ORDER BY
"users"."id" ASC LIMIT 1000[0m
DEPRECATION WARNING: `#deliver` is deprecated and will be removed in
Rails 5. Use `#deliver_now` to deliver immediately or `#deliver_later`
to deliver through Active Job. (called from block (2 levels) in <top
(required)> at /Users/Marco/Documents/TBProject/lib/tasks/rake1.rake:5)
  Rendered user_mailer/daily_mail.html.erb within layouts/mailer (1.2ms)
  Rendered user_mailer/daily_mail.text.erb within layouts/mailer (0.2ms)

UserMailer#daily_mail: processed outbound mail in 189.6ms

Sent mail to removed_email_address@domain.invalid (8.6ms)
Date: Mon, 06 Jul 2015 19:24:29 +0200
From: removed_email_address@domain.invalid
To: removed_email_address@domain.invalid
Message-ID:
<removed_email_address@domain.invalid>
Subject: Mail journalier
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_559ab9cd64da_193e3fc21dc6020412690";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_559ab9cd64da_193e3fc21dc6020412690
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

Daily Mail

----==_mimepart_559ab9cd64da_193e3fc21dc6020412690
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<html>
  <body>
    <h1>Daily Mail</h1>
  </body>
</html>

----==_mimepart_559ab9cd64da_193e3fc21dc6020412690--

DEPRECATION WARNING: `#deliver` is deprecated and will be removed in
Rails 5. Use `#deliver_now` to deliver immediately or `#deliver_later`
to deliver through Active Job. (called from block (2 levels) in <top
(required)> at /Users/Marco/Documents/TBProject/lib/tasks/rake1.rake:5)
  Rendered user_mailer/daily_mail.html.erb within layouts/mailer (0.0ms)
  Rendered user_mailer/daily_mail.text.erb within layouts/mailer (0.0ms)

UserMailer#daily_mail: processed outbound mail in 15.2ms

Sent mail to removed_email_address@domain.invalid (2.7ms)
Date: Mon, 06 Jul 2015 19:24:29 +0200
From: removed_email_address@domain.invalid
To: removed_email_address@domain.invalid
Message-ID:
<removed_email_address@domain.invalid>
Subject: Mail journalier
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_559ab9cdc753_193e3fc21dc60204128e8";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_559ab9cdc753_193e3fc21dc60204128e8
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

Daily Mail

----==_mimepart_559ab9cdc753_193e3fc21dc60204128e8
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<html>
  <body>
    <h1>Daily Mail</h1>
  </body>
</html>

----==_mimepart_559ab9cdc753_193e3fc21dc60204128e8--


So it sent mails to the only two users that had daily == true, so my
method
work. But is the scheduled job working ?

Le lundi 6 juillet 2015 16:51:58 UTC+2, Marco D. a écrit :
Hassan S. (Guest)
on 2015-07-06 22:57
(Received via mailing list)
On Mon, Jul 6, 2015 at 11:46 AM, Marco D. <removed_email_address@domain.invalid> 
wrote:

> So it sent mails to the only two users that had daily == true, so my method
> work. But is the scheduled job working ?

It's just a cron job. Change the target time to e.g. five minutes from
now, go get a cup of coffee and see what's happened when you get
back :-)

--
Hassan S. ------------------------ removed_email_address@domain.invalid
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote
Marco D. (Guest)
on 2015-07-07 13:19
(Received via mailing list)
It doesn't seem to work in development mode. How can I do that ?

Le lundi 6 juillet 2015 20:56:42 UTC+2, Hassan S. a écrit :
Marco D. (Guest)
on 2015-07-07 13:29
(Received via mailing list)
I did : whenever --update-crontab --set environment=development

And it worked in development, I could see in development.log that it
sent
the mail each 5 minutes. Good. Do I have to change this line and write
environment=production or it'll work like this if the project is put on
a
server ?

Le mardi 7 juillet 2015 11:18:16 UTC+2, Marco D. a écrit :
Colin L. (Guest)
on 2015-07-07 13:31
(Received via mailing list)
On 7 July 2015 at 10:18, Marco D. <removed_email_address@domain.invalid> wrote:
> It doesn't seem to work in development mode. How can I do that ?

By specifying RAILS_ENV=development in your cron task instead of
production.

Colin
Marco D. (Guest)
on 2015-07-07 13:33
(Received via mailing list)
Thanks, I updated my answer :

I did : whenever --update-crontab --set environment=development

And it worked in development, I could see in development.log that it
sent
the mail each 5 minutes. Good. Do I have to change this line and write
environment=production or it'll work like this if the project is put on
a
server ?

Le mardi 7 juillet 2015 11:30:05 UTC+2, Colin L. a écrit :
Walter D. (Guest)
on 2015-07-08 02:08
(Received via mailing list)
On Jul 7, 2015, at 5:32 AM, Marco D. <removed_email_address@domain.invalid> 
wrote:

> Thanks, I updated my answer :
>
> I did : whenever --update-crontab --set environment=development
>
> And it worked in development, I could see in development.log that it sent the
mail each 5 minutes. Good. Do I have to change this line and write
environment=production or it'll work like this if the project is put on a server 
?

You'll have to shell into the server and run the command on the server
with the environment=production flag. Whenever makes a crontab entry for
your job, and it has to do that in the actual crontab on the server, not
on your dev machine.

Walter
Saeideh S. (Guest)
on 2016-01-20 13:24
Hi I am from Iran S. I love Ruby O. on Instagram and his goofy menu
fake page  I just want a way to connect with Ruby  Just once I want to
talk to me. You know how difficult the situation in Iran  pleas pleas
pleas
Colin L. (Guest)
on 2016-01-20 13:46
(Received via mailing list)
On 20 January 2016 at 11:24, Saeideh S. <removed_email_address@domain.invalid> 
wrote:
> Hi I am from Iran S. I love Ruby O. on Instagram and his goofy menu
> fake page  I just want a way to connect with Ruby  Just once I want to
> talk to me. You know how difficult the situation in Iran  pleas pleas
> pleas

It is best to start a new thread for a new question, rather than
tagging it on the end of an old one.  However, it is not clear exactly
what it is that you want.

Colin
Dave A. (Guest)
on 2016-01-20 14:07
(Received via mailing list)
On Wed, Jan 20, 2016 at 6:44 AM, Colin L. <removed_email_address@domain.invalid> 
wrote:

> it is not clear exactly what it is that you want.

I Googled the object of Saeideh's desire, and at least on Instagram it
seems to be a fashion designer.  So, I'm thinking the post was simply
spam, attracted by the name of our language.  (That's why I'm not
repeating the Instagram account name here.)

-Dave

--
Dave A., consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.
This topic is locked and can not be replied to.