联系表格邮件在Rails 4 [英] Contact Form Mailer in Rails 4
问题描述
我正在尝试在Rails 4中构建一个联系表单,该表单将姓名,电子邮件和正文发送到我的电子邮件地址。单击提交后,应用程序将正确重定向回联系人页面,但似乎没有发送电子邮件。
I am trying to build a contact form in Rails 4, where the form takes a name, email, and body and sends it to my email address. Upon clicking "Submit", the app redirects back to the Contact page correctly, but no email appears to get sent.
match '/send_mail', to: 'contact#send_mail', via: 'post'
contact_email.html.erb
contact_email.html.erb
<!DOCTYPE html>
<html>
<head>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p>You have received the following email from <%= "#{ @name } (#{ @email }):" %></p>
<p><%= @body %></p>
</body>
</html>
contact_controller.rb
contact_controller.rb
def send_mail
name = params[:name]
email = params[:email]
body = params[:comments]
ContactMailer.contact_email(name, email, body).deliver
redirect_to contact_path, notice: 'Message sent'
end
contact_mailer.rb
contact_mailer.rb
class ContactMailer < ActionMailer::Base
default to: # my email address
def contact_email(name, email, body)
@name = name
@email = email
@body = body`enter code here`
mail(from: email, subject: 'Contact Request')
end
end
contact.html.erb
contact.html.erb
<div class="container-content">
<div class="container">
<%= form_tag(send_mail_path) do %>
<div class="form-group">
<%= label_tag 'name', 'Name' %>
<%= text_field_tag 'name', nil, class: 'form-control', placeholder: 'Your Name' %>
</div>
<div class="form-group">
<%= label_tag 'email', 'Email' %>
<%= email_field_tag 'email', nil, class: 'form-control', placeholder: 'Your Email Address' %>
</div>
<div class="form-group">
<%= label_tag 'comments', 'Comments' %>
<%= text_area_tag 'comments', nil, class: 'form-control', rows: 4, placeholder: 'Comments...' %>
</div>
<%= submit_tag nil, class: 'btn btn-default btn-about pull-right' %>
<% end %>
</div>
</div>
application.rb
application.rb
config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
推荐答案
原来,我没有使用外部SMTP服务配置Heroku应用程序(因为我从来没有做任何事情之前发送电子邮件,我不知道这样做)。由于我比较熟悉MailChimp,尤其是由于他们的Mandrill服务具有免费层(我正在为学生组织构建此应用程序),因此我轻松地将Mandrill添加到我的Heroku应用程序中,并在应用程序中包含以下设置。 rb :
It turned out that I hadn't configured my Heroku app with an external SMTP service (since I have never done anything with email before, I didn't know to do this). Since I'm relatively familiar with MailChimp, and especially since their Mandrill service has a free tier (I am building this app for a student organization), I easily added Mandrill to my Heroku app and included the following settings in application.rb:
ActionMailer::Base.smtp_settings = {
address: 'smtp.mandrillapp.com',
port: 587,
user_name: ENV['MANDRILL_USERNAME'],
password: ENV['MANDRILL_APIKEY']
}
其中附件自动设置ENV变量的地方。
Where the ENV vars were set automatically by the add-on.
这篇关于联系表格邮件在Rails 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!