1

I can't seem to make the whenever gem work with my actionmailer. I am trying to run the application in development wherein an email would be sent to a specific individual at a specific time.

In my schedule.rb file I have the following:

every :day, :at => '12:48pm' do
  runner "FoodPlan.food_email"
end

In my controller called food_plans_controller.rb I have:

  def self.food_email
    @food_plans = FoodPlan.where(food_plan_date: Date.today).order('meal ASC')

    UserMailer.food_email(@food_plans).deliver_now
  end

In user_mailer.rb I have (take note I removed the email for privacy reasons) :

  def food_email (food)
    @food_plans = food

    mail(to: '---------@yahoo.com', subject: 'Food Orders for #{Date.today}')
  end

I have a folder in the views called user_mailer, inside it is a file called food_email.html.erb with the ff:

<!DOCTYPE html>
<html>    
</head>
  <body>
    <h1>title</h1>
    <p>
      Good morning, -----!
      <br><br>
      Meal Plans for <%=Date.today%>:
      <br><br>
      <table class="table table-bordered table-striped">
  <thead>
    <tr>
      <th>#</th>
      <th>Meal Plan</th>
      <th>Ordered by</th>
      <th>Room Number</th>
      <th>Received by</th>
      <th>Signature</th>
    </tr>
  </thead>

  <tbody>
    <%countint=1%>
    <% @food_plans.each do |food_plan| %>
      <tr>
        <td><%=countint%></td>
        <td><%= food_plan.meal %></td>
        <td><%=food_plan.applicant.first_name%>
        <%=food_plan.applicant.last_name%></td>
        <td><%=food_plan.applicant.room_number%></td>
        <%countint+=1%>
        <td></td>
        <td></td>
      </tr>
    <% end %>


  </tbody>
</table>
      <br>
      <br>
      ---.
      <br>
      <br>
      If you have any concerns, don't hesitate to call us at ------.

      <br>
      <br>
      Thanks, <br>----
    </p>
  </body>
</html> 

In my development config I have (I removed the email and password):

  config.action_mailer.smtp_settings = {
  address:              'smtp.gmail.com',
  port:                 587,
  domain:               'example.com',
  user_name:            '-------',
  password:             '-------',
  authentication:       'plain',
  enable_starttls_auto: true  }

I have tried reading this guide but I cannot still get the gem to work with actionmailer. I have also read up on the documentation of the whenever gem but I still can't figure out what I'm doing wrong. :(

I'm not getting any errors, it's just that the email is not sending.

Acrux
  • 386
  • 3
  • 26
  • Specifically what doesn't work and what issue are you having? Are you getting an error? Is the mail not being sent even though you expect it to? – Anthony E May 15 '16 at 05:09
  • The email is not being sent. I don't think that it's even going into the scheduler. I'm looking at the logs of my localhost terminal and there aren't any changes to the logs even during the time I specified in the scheduler. – Acrux May 15 '16 at 05:12

1 Answers1

1

I guess your whenever rules did not ever get to the local crontab and thus are never actually run. The whenever gem rules do not make the system run the commands by theselves, they are only a ruby notation of the cron rules that reside in the /etc/crontab (or similar) on unix-like systems.

The whenever gem automatically updates the crontab during deployment (using the capistrano plugin), so your rules should work on the production server.

On the development host however, you need to update your crontab manually (credits):

whenever --update-crontab --set environment='development'

To view what's currently inside your crontab, use cat /etc/crontab. Your rules, defined in whenever, should be present in the file after you've updated it.

Community
  • 1
  • 1
Matouš Borák
  • 15,606
  • 1
  • 42
  • 53