我在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"] = 'mark@doc.org.uk'
@body["message"] = message
@recipients = recipient
@from = 'no-reply@doc.org.uk'
@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
,但我已经给出了它的位置。任何有关如何解决此错误的线索都将非常感激。
答案 0 :(得分:2)
看起来这一行是问题所在:
@params["email"]
如果它是表格中的数据,请删除@。
答案 1 :(得分:1)
@params isint在您的控制器中初始化。
你可能很想简单地使用params获取你的http动作参数。