Acrux
Acrux

Reputation: 406

Whenever Gem Daily email using actionmailer not sending

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: '[email protected]', 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.

Upvotes: 1

Views: 518

Answers (1)

Matouš Bor&#225;k
Matouš Bor&#225;k

Reputation: 15954

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.

Upvotes: 1

Related Questions