2011-07-04 89 views
0

我已經在我的Ruby on Rails應用程序中創建了一個用戶數據庫,現在我正在嘗試創建一個郵件程序,可以在我需要的時候向我的數據庫中的所有用戶發送電子郵件。Mailer:使用Ruby on Rails失敗發送電子郵件

這裏是我的模型:

class MailMessage < ActionMailer::Base 

    def contact(recipient, subject, message) 
    # host = Hobo::Controller.request_host 
    # app_name = Hobo::Controller.app_name || host 
    @subject = subject 
    # @body  = { :user => user, :host => host, :app_name => app_name } 
    @body["title"] = 'This is title' 
    @body["email"] = '[email protected]' 
    @body["message"] = message 
    @recipients = recipient 
    @from  = '[email protected]' 
    @sent_on = Time.now 
    @headers = {} 
    end 
end 

這裏是我的控制器:

class MailMessageController < ApplicationController 
    def sendmail 
    email = @params["email"] 
    recipient = email["recipient"] 
    subject = email["subject"] 
    message = email["message"] 
    MailMessage.deliver_contact(recipient, subject, message) 
    return if request.xhr? 
    render :text => 'Message sent successfully' 
    end 

    def index 
    render :file => 'app/views/mail_message/index.html' 
    end 
end 

這裏是我的views/mail_message

<h1>Send Email</h1> 

<%= form_tag :action => 'sendmail' %> 
    <p> 
    <label for="email_subject">Subject</label> 
    <%= text_field 'email', 'subject' %> 
    </p> 
    <p> 
    <label for="email_recipient">Recipient</label> 
    <%= text_field 'email', 'recipient' %> 
    </p> 
    <p> 
    <label for="email_message">Message</label> 
    <%= text_area 'email', 'message' %> 
    </p> 
    <%= submit_tag "Send" %> 
<%= form_tag %> 

這裏是我的enviroment.rb

ActionMailer::Base.delivery_method = :sendmail 
    ActionMailer::Base.sendmail_settings = { 
     :location  => '/usr/sbin/sendmail', 
     :arguments  => '-i -t' 
    } 

    ActionMailer::Base.perform_deliveries = true # the "deliver_*" methods are available 
    ActionMailer::Base.raise_delivery_errors = true 
    ActionMailer::Base.default_charset = "utf-8" 
    ActionMailer::Base.default_content_type = "text/html" # default: "text/plain" 
    ActionMailer::Base.default_mime_version = "1.0" 
    ActionMailer::Base.default_implicit_parts_order = [ "text/html", "text/plain"] 

當我運行一個測試消息,我得到以下錯誤:

You have a nil object when you didn't expect it! 
You might have expected an instance of ActiveRecord::Base. 
The error occurred while evaluating nil.[] 
app/controllers/mail_message_controller.rb:4:in `sendmail' 

它似乎並沒有認識到sendmail,但我已經給它的位置。任何線索如何解決這個錯誤將非常感激。

回答

2

它看起來像這條線的問題是:

@params["email"] 

如果它的意思是從表單中的數據,刪除@。

+0

是的,謝謝,現在工作得很好。 – Mark

1

@params isint在您的控制器中初始化。

你可能很簡單想使用params來獲取你的http動作參數。

相關問題