我有一个带有现有联系表格的rails 4.2 app。如何通过电子邮件将电子邮件发送到包含该邮件的地址? mail_form gem看起来最有希望,但当我尝试从控制台发送电子邮件时,我得到了一个错误的'尝试传递时的消息。
表格如下:
<%= form_for(enquiry) do |f| %>
<% if enquiry.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(enquiry.errors.count, "error") %> prohibited this enquiry from being saved:</h2>
<ul>
<% enquiry.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :subject, "Subject:" %><br>
<%= f.text_field :subject, :size => "40" %>
</div>
<div class="field">
<%= f.label :e_description, "Description:" %><br>
<%= f.text_area :e_description, :cols => "80", :rows => "10" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我刚从README获得了基本的ContactForm模型:
class ContactForm < MailForm::Base
attribute :customer_name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :message
# Declare the e-mail headers. It accepts anything the mail method
# in ActionMailer accepts.
def headers
{
:subject => "My Contact Form",
:to => "my.email@gmail.com",
:from => %("#{customer_name}" <#{email}>)
}
end
end
编辑 - 咨询控制员:
class EnquiriesController < ApplicationController
def index
@enquiries = Enquiry.all
end
def show
end
def new
@enquiry = Enquiry.new
render 'pages/contactus'
end
def edit
end
def create
@enquiry = Enquiry.new(enquiry_params)
if current_user.customer?
@enquiry.user_id = current_user.id
end
if @enquiry.save
redirect_to '/pages/contactus', notice: 'Enquiry was successfully sent.'
else
render :new
end
end
def update
if @enquiry.update(enquiry_params)
redirect_to @enquiry, notice: 'Enquiry was successfully updated.'
else
render :edit
end
end
def destroy
@enquiry.destroy
redirect_to enquiries_url, notice: 'Enquiry was successfully destroyed.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_enquiry
@enquiry = Enquiry.find(params[:id])
end
# If resource not found redirect to root and flash error.
def resource_not_found
yield
rescue ActiveRecord::RecordNotFound
redirect_to root_url, :notice => "Room not found."
end
# Only allow a trusted parameter "white list" through.
def enquiry_params
params.require(:enquiry).permit(:subject, :e_description, :user_id)
end
end
答案 0 :(得分:1)
<div class="field">
<%= f.label :e_description, "Description:" %><br>
<%= f.text_area :e_description, :cols => "80", :rows => "10" %>
</div>
我认为这是您的电子邮件的内容。表单调用它为e_description,而在ContactForm类中,我在第5行看到对消息的引用。使它们相同。
添加控制器代码有助于进一步分析。
另一个问题是提及电子邮件&#39;第13行(应该生成错误,因为它应该包含在引号内。)尝试;
:from => "#{customer_name} <#{email}>"