繁体   English   中英

Mailer:在 Rails 上使用 Ruby 发送电子邮件失败

[英]Mailer: Sending emails using Ruby on Rails failing

我在我的 Ruby on Rails 应用程序中创建了一个用户数据库,现在我正在尝试创建一个邮件程序,可以随时向我数据库中的所有用户发送电子邮件。

这是我的 model:

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

这是我的 controller:

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 ,但我已经给出了它的位置。 任何有关如何解决此错误的线索将不胜感激。

看起来这条线是问题所在:

@params["email"]

如果它是表单中的数据,请删除 @。

@params isint 在您的 controller 中初始化。

您可能很想使用参数来获取 http 操作参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM