2015-01-07 60 views
0

我正在一個電子郵件調度程序/發件人在Ruby on Rails應用程序。我使用Bunny gem創建消息隊列,並且我有一個定期將消息放入隊列的調度程序。我正在使用一個Sneakers rake任務來添加隊列中的消息。我開始在命令行的運動鞋的工人,像這樣:ActionMailer在運動鞋耙任務

WORKERS=Processor rake sneakers:run 

代碼到達mail()函數,然後退出,甚至不產生電子郵件模板。我在正常的應用程序中使用電子郵件代碼,並將配置轉移到rake任務,所以我知道我的配置是正確的。

應用程序/工人/ 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 

應用程序/郵件/ 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 

應用程序/視圖/ 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> 

配置/環境/ 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} 

任何幫助將不勝感激!

編輯1: 將模板移動到正確的位置,但模板仍然不呈現,並且電子郵件未發出。

+0

您是否檢查過日誌中是否有任何可能提供有關正在發生的線索的錯誤? – trueinViso

+0

剛剛發現該模板位於錯誤的位置,所以我解決了這個問題。當我再次運行它時,仍然沒有發送電子郵件,但缺少的模板錯誤消失了。我在mail()函數之前看到我的記錄,之後沒有提到要渲染的模板。 –

+0

我對你正在使用的寶石不太瞭解,但也許電子郵件坐在某個隊列中。 – trueinViso

回答

0

現在我已經從使用不斷將消息從隊列中取出的Sneakers工作者切換到一個計劃rake任務,該任務將隊列中的所有可用消息轉換。我不知道爲什麼Action Mailer在運動鞋工作人員與Rake Task中表現如此不同,但這是一個體面的解決方法,並且有大量關於使用Rufus創建郵件程序rake任務的文檔。如果有人有任何見解,請張貼它,因爲我仍然好奇如何讓運動鞋工人使用動作郵件程序。