Sending mails automatically through whenever and action mailer Rails 3.1

2,994 views
Skip to first unread message

Akram

unread,
Dec 7, 2011, 8:16:53 AM12/7/11
to Whenever Gem
I have to send weekly emails to all the user about the latest things
happening. I am using ActionMailer to accomplish other mailing task
however I have no clue how to automate the weekly emails.

in schedule.rb


every :wednesday, :at => '3pm' do
runner "UsersMailer.weekly_mail"
end

in users_mailer.rb

default :to => User.all.map(&:email)


def weekly_mail
mail(:subject => "Weekly email from footyaddicts")
end

However I am not getting any mails. Any clue what might be wrong?

Thanks,

Javan Makhmali

unread,
Dec 7, 2011, 9:03:55 AM12/7/11
to whenev...@googlegroups.com
There's a couple issues here. You probably don't want to send an email to all of your users at once using the To: field.

I'd create a class method in your user model that loops over each user, builds up a message, and then delivers it. Read the action mailer docs (http://api.rubyonrails.org/classes/ActionMailer/Base.html) specifically the "Sending mail" section.

Then call that class method in your cron job, not the mailer itself.

Hope that helps some.

-Javan

--
You received this message because you are subscribed to the Google
Groups "Whenever Gem" group and because you're awesome.
To post to this group, send email to whenev...@googlegroups.com
To unsubscribe from this group, send email to
whenever-gem...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/whenever-gem?hl=en

Akram

unread,
Dec 7, 2011, 9:54:32 AM12/7/11
to Whenever Gem
Hi Javan,

As per your suggestion, I made changes in the code and here is how it
looks right now :-

1) scheduler.rb

every :wednesday, :at => "8:20pm" do
runner "User.weekly_update"
end

2) user.rb

def weekly_update
@user = User.all
@user.each do |u|
UsersMailer.weekly_mail(u.email).deliver
end
end

3) users_mailer.rb

def weekly_mail(email)
mail(:to => email:subject => "Weekly email from footyaddicts")
end

is there something which I am doing terribly wrong?

Thanks

Javan Makhmali

unread,
Dec 7, 2011, 9:57:54 AM12/7/11
to whenev...@googlegroups.com
`def weekly_update` should be `def self.weekly_update` to make it a class method.

I also suggest you run User.weekly_update from the console first to help spot any syntax errors. Or even better, write a unit test.

-Javan

Akram

unread,
Dec 7, 2011, 10:39:54 AM12/7/11
to Whenever Gem
Running User.weekly_update from console works just fine and I am able
to see the mails. However I am not able to get the mails automatically
at the specified time.

I am testing it in the development environment.

Thanks,

Javan Makhmali

unread,
Dec 7, 2011, 10:43:11 AM12/7/11
to whenev...@googlegroups.com
Run `crontab -l` on your server to see if the cron job was written correctly. Also keep in mind that "8:20pm" will be 8:20 according to your server, not your local time.

Akram

unread,
Dec 7, 2011, 10:54:39 AM12/7/11
to Whenever Gem
crontab -l gives me this information

0,10,20,30,40,50 * * * * /bin/bash -l -c 'cd /home/akram/RoR/f &&
script/rails runner -e production '\''User.weekly_update'\'''

i tried to change the environment by whenever --set
environment=development and then did whenever --update-crontab f

when i tried to see if the crontab job was written correctly crontab -
l gave me the same information again i.e

0,10,20,30,40,50 * * * * /bin/bash -l -c 'cd /home/akram/RoR/f &&
script/rails runner -e production '\''User.weekly_update'\'''

Thanks,

Javan Makhmali

unread,
Dec 7, 2011, 11:13:52 AM12/7/11
to whenev...@googlegroups.com
Try running that command on your server manually:

/bin/bash -l -c 'cd /home/akram/RoR/f && script/rails runner -e production '\''User.weekly_update'\'''

Akram

unread,
Dec 7, 2011, 11:18:13 AM12/7/11
to Whenever Gem
it gives this error :-

Access denied for user 'production'@'localhost' (using password: YES)
(Mysql2::Error)

Javan Makhmali

unread,
Dec 7, 2011, 11:21:33 AM12/7/11
to whenev...@googlegroups.com
Ok, there's your problem. Does the app work in a browser? Are you able to get in to a production console on the server?

This is well out of the realm of Whenever now so good luck!

-Javan

Akram

unread,
Dec 7, 2011, 11:29:18 AM12/7/11
to Whenever Gem
It actually works in a browser, moreover I was trying to use Whenever
in development mode.

Thanks for your help

Akram

Akram

unread,
Dec 7, 2011, 12:01:46 PM12/7/11
to Whenever Gem
one last thing

when i change the environment to development and run the same command

/bin/bash -l -c 'cd /home/akram/RoR/f && script/rails runner -e

development '\''User.weekly_update'\'''

I am able to get the mail.

Javan Makhmali

unread,
Dec 7, 2011, 12:05:38 PM12/7/11
to whenev...@googlegroups.com
Sounds like your database.yml has credentials for development, but not production.

ccl.l...@gmail.com

unread,
Dec 4, 2012, 10:17:38 AM12/4/12
to whenev...@googlegroups.com
Hi all,

I have a very similar issue : I want to send a mail every sunday to all the user of my app, so I planed to do it with whenever.
In my model user.rb:
def self.mail_recap_semaine
  
@user = User.all
  @user.each do |u|

    UserMailer.mail_recap_semaine(u.email).deliver
  end
end
user_mailer.rb
def mail_recap_semaine(email)
    mail(:to => email, :subject => "Weekly email from footyaddicts")
  end
schedule.rb
every 10.minutes do
   runner "User.mail_recap_semaine"
end

I run the crontab -l
# Begin Whenever generated tasks for: miiaou
0,10,20,30,40,50 * * * * /bin/bash -l -c 'cd /home/miiaou/miiaou && script/rails runner -e production '\''User.mail_recap_semaine'\'''
# End Whenever generated tasks for: miiaou

and when I launch User.mail_recap_semaine throught the rails console, it works.

But when I try to launch
/bin/bash -l -c 'cd /home/miiaou/miiaou && script/rails runner -e production '\''User.mail_recap_semaine'\'''
directly in the bash, I have the error : 
/usr/bin/env: ruby.exe: Aucun fichier ou dossier de ce type (= no file or directory)

I don't know how fix it.
Any idea?

Javan Makhmali

unread,
Dec 4, 2012, 10:36:14 AM12/4/12
to whenev...@googlegroups.com
Was this app generated on a Windows machine? Sounds like the shebang in your script/runner file points to ruby.exe.

-Javan

ccl.l...@gmail.com

unread,
Dec 4, 2012, 10:37:39 AM12/4/12
to whenev...@googlegroups.com
Yes, I work on windows but the server is a debian

Javan Makhmali

unread,
Dec 4, 2012, 2:01:09 PM12/4/12
to whenev...@googlegroups.com
You'll need to update the shebang in your script/runner to something like: #!/usr/bin/env ruby

ccl.l...@gmail.com

unread,
Dec 4, 2012, 2:45:58 PM12/4/12
to whenev...@googlegroups.com
Ok.
Stupid question, but what is the shebang, and how can I update it?

Javan Makhmali

unread,
Dec 4, 2012, 2:47:34 PM12/4/12
to whenev...@googlegroups.com
It's line 1 of the script/runner file in your app. It probably currently reads: #!/usr/bin/env: ruby.exe



On Dec 4, 2012, at 2:45 PM, ccl.l...@gmail.com wrote:

Ok.
Stupid question, but what is the shebang, and how can I update it?

ccl.l...@gmail.com

unread,
Dec 4, 2012, 3:08:21 PM12/4/12
to whenev...@googlegroups.com
Thanks!

And if I want to change the environment to development, I just need to do:
whenever --update-crontab appname --set environment=development
, right?

Javan Makhmali

unread,
Dec 4, 2012, 3:41:35 PM12/4/12
to whenev...@googlegroups.com
Yup, looks good.

ccl.l...@gmail.com

unread,
Dec 6, 2012, 8:30:02 AM12/6/12
to whenev...@googlegroups.com
Works perfectly. Thanks a lot!
Reply all
Reply to author
Forward
0 new messages