0
votes

I'm putting together an email scheduler/sender in a Ruby on Rails application. I am using the Bunny gem for creating a messaging queue, and I have a Rufus scheduler that periodically puts messages into the queue. I am using a Sneakers rake task to pull in messages from the queue whenever they are added. I start the Sneakers worker from the command line like so:

WORKERS=Processor rake sneakers:run

The code reaches the mail() function and then quits, the email template isn't even being generated. I have working email code in my normal application and I transferred the configurations over to the rake task, so I know my configurations are correct.

app/workers/processor.rb

require 'sneakers'
require 'json'
require 'action_mailer'

class Processor
  include Sneakers::Worker
  from_queue :email_queue,
        :env => 'development',
    :ack => true
  Sneakers.configure {}
  Sneakers.logger.level = Logger::ERROR

  Sneakers::Worker.configure_logger(Logger.new('/dev/null'))

  def work(msg)
    string  = msg.force_encoding("ISO-8859-1")
    hash = JSON.parse(string)
    ack!
    UserMailer.test_email(hash).deliver
  end
end

app/mailer/user_mailer.rb

require 'action_mailer'
require 'fog'
require 'rubygems'

class UserMailer < ActionMailer::Base
  def test_email(hash)
    @order = hash["order"]
    @currentUser = hash["user"]
    @staffCompany = hash["company"]
    mail(to: "[email protected]", from: "[email protected]", subject: 'Action Mailer')
  end
end

app/views/user_mailer/test_email.html.erb

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
  </head>
  <body>
    <div class="PlainText">
      Some text goes here
    </div>
  </body>
</html>

config/environments/development.rb

config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address: 'smtp.gmail.com',
  port: 587,
  domain: 'gmail.com',
  user_name: 'gmail account',
  password: 'password',
  authentication: 'plain',
  enable_starttls_auto: true}

Any help would be greatly appreciated!

edit 1: Moved the template to the correct location, but the template still isn't rendering and the email isn't being sent out.

2
Have you checked the logs for any errors that might give a clue as to what is happening?trueinViso
Just found that the template was in the wrong location, so I fixed that. When I ran it again it still isn't sending out the email, but the missing template error is gone. I see my logging right before the mail() function, then after that there is no mention of a template being rendered.Brian Smith
I don't know much about the gems you are using, but maybe the email is sitting in a queue somewhere.trueinViso

2 Answers

1
votes

At the risk of reviving an old thread, I came across this on Google researching a similar problem. For anyone else's sake that does the same, here's a passing thought.

I noticed in your worker you call the mailer after you call ack!. The documentation (see Job Control after the list) says that this breaks a reporting paradigm since the last line in the worker needs to return a certain value to manage the queue properly. I wonder if what you're seeing is a side-effect of that.

In your app/workers/processor.rb, Try changing:

  def work(msg)
    string  = msg.force_encoding("ISO-8859-1")
    hash = JSON.parse(string)
    ack!
    UserMailer.test_email(hash).deliver
  end

To:

  def work(msg)
    string  = msg.force_encoding("ISO-8859-1")
    hash = JSON.parse(string)
    UserMailer.test_email(hash).deliver
    ack!
  end
0
votes

For now I have switched from using a Sneakers worker that is constantly taking messages from the queue, to a scheduled rake task that takes all available messages in the queue. I don't know why the Action Mailer performs so differently in the Sneakers worker versus the Rake Task, but this is a decent workaround and there is plenty of documentation on creating a mailer rake task using Rufus. If anyone has any insight please post it as I'm still curious how to get the Sneakers worker to use the Action Mailer.