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.
routes.rb
match '/send_mail', to: 'contact#send_mail', via: 'post'
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
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
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
<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
config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
See Question&Answers more detail:
os