梅勒:电子邮件是空的,无法弄清楚原因

时间:2013-06-30 12:01:52

标签: ruby-on-rails

所以,有控制器方法:

  def send_feedback
  if params[:f_message].length!=0
  if current_user && current_user.email
    UserMailer.feedback_email(current_user.email, params[:f_name], params[:f_message]).deliver
    redirect_to root_url,  :notice=>"Thank you for contacting us"
  else
    UserMailer.feedback_email(params[:f_email], params[:f_name], params[:f_message]).deliver
    redirect_to root_url,  :notice=>"Thank you for contacting us"
  end
  else
  redirect_to "/info/feedback", :notice=>"Your message is empty... Please be a little bit more informative"
  end
  end

服务器登录提交反馈表:

Started POST "/info/send_feedback" for 127.0.0.1 at 2013-06-30 15:50:23 +0400
Processing by InfoController#send_feedback as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"9yht/zb6RqAlpOJeLPTKpAxvINzWAqFJJHkZE+6hv1Q=", "f_email"=>"", "f_name"=>"", "f_message"=>"test" }

(所以我得到params并将它们传递给feedback_email):

  def feedback_email(email, name, message)
    mail :to => "support@*******.com", :subject => "feedback"
    @mail=email
    @name=name
    @message=message
  end

使用模板:

From: <%=@name.inspect %>
Mail: <%= @mail.inspect %>
<%= @message.inspect %>

最后,发送电子邮件的服务器日志:

Sent mail to *** (2045ms)
Date: Sun, 30 Jun 2013 15:50:23 +0400
From: from@example.com
To: support@*******.com
Message-ID: <51d01b7f2a183_15902ec382492cf@HP-PC.mail>
Subject: feedback
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit
From: nil
Mail: nil
nil

Redirected to http://localhost:3000/

但是,它是空的。为什么呢?

1 个答案:

答案 0 :(得分:1)

您必须在发送电子邮件之前设置变量,而不是在:

之后
def feedback_email(email, name, message)
  @mail    = email
  @name    = name
  @message = message

  mail :to => "support@*******.com", :subject => "feedback"
end