2011-04-11 96 views
0

我有一張電子郵件表。我需要每個用戶都收到電子郵件。需要發送電子郵件的幫助

所以我提出:

script/generate mailer Notifier 

下一步。

class Notifier < ActionMailer::Base 
    def newgrants_notification(respondent) 
    recipients user.email 
    from  "[email protected]" 
    subject "Hi!" 
    body  (:respondent => respondent) 
    end 
end 

app/views/notifier/newgrants_notification.erb 寫道:Hello!

,這我的控制器,其中i創建問題

@question = Question.create(:text => params[:question][:text], :security => rand(888).to_i) 

if success = @question.save 
    respondents = Respondent.find(:all) 
    respondents.each do |res| 
    Inquiry.create(:question_id=>@question.id.to_i, :respondent_id=>res.id.to_i) 
    Notifier.newgrants_notification(respondents).deliver #this is right?? 
    end 

我做了什麼錯誤?消息不會來;(

回答

4

HI

respondents.each do |res| 
    Inquiry.create(:question_id=>@question.id.to_i, :respondent_id=>res.id.to_i) 
    Notifier.newgrants_notification(res).deliver 
end 

當您發送郵件時,郵件ID將作爲參數傳遞,因此應答者將被替換爲res。

+0

謝謝,現在我明白了,只有當項目處於生產模式時,我纔會收到電子郵件? – 2011-04-11 07:35:13

+0

收件人user.email也檢查此 – Bijendra 2011-04-11 07:35:15

+0

http://stackoverflow.com/questions/5485348/how-does-actionmailer-work/5485972#5485972 – Bijendra 2011-04-11 07:43:00

1

你正在通過你的數組的響應者,當你使用每個,管道中的變量(| res |)是一個用來引用循環中的單數對象。

Notifier.newgrants_notification(res).deliver 
+0

謝謝!我錯過了這部分! – 2011-04-11 07:33:43

1

在開發模式下,所有的電子郵件都不會發,這一切都記錄只在您的日誌文件。所以,如果你在這個環境中進行測試是正常的。檢查你的日誌,如果你看到它:)

+0

噢,是的,我看到這:)謝謝你,在日誌中我看到:「發送郵件到[email protected]」,所以所有工作都很完美? – 2011-04-11 07:32:16

+0

它工作正常。現在您需要測試您的電子郵件配置。其作品。但檢查你的sendmail或你的smtp,看看是不是他他們有一些錯誤。 – shingara 2011-04-11 07:37:14

+0

不錯。我不會寫任何地方smtp; o它不好?如果是,我應該在哪裏寫?現在工作正常,如果我有5個電子郵件在數據庫中,在日誌中我收到5封電子郵件。好! – 2011-04-11 07:41:56