Я пытаюсь настроить контактную форму для веб-сайта. Кажется, в консоли все работает нормально, но я не получаю никаких писем. Может ли кто-нибудь сказать мне, что я делаю неправильно? Я проверил другие ответы, но они не решают мою проблему.
Консоль:
Loading development environment (Rails 4.2.0)
irb(main):001:0> c = Contact.new(:name => "name", :email => "[email protected]", :message => "hi")
=> #<Contact:0x007ffdbbc16360 @name="name", @email="[email protected]", @message="hi">
irb(main):002:0> c.deliver
DEPRECATION WARNING: `#deliver` is deprecated and will be removed in Rails 5. Use `#deliver_now` to deliver immediately or `#deliver_later` to deliver through Active Job. (called from irb_binding at (irb):2)
Rendered /Users/user/.rvm/rubies/ruby-2.1.3/lib/ruby/gems/2.1.0/gems/mail_form-1.5.0/lib/mail_form/views/mail_form/contact.erb (10.8ms)
MailForm::Notifier#contact: processed outbound mail in 204.0ms
Sent mail to [email protected] (7.3ms)
Date: Tue, 17 Feb 2015 10:44:49 +0000
From: name <[email protected]>
To: [email protected]
Message-ID: <[email protected]>
Subject: My Contact Form
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<h4 style="text-decoration:underline">My Contact Form</h4>
<p><b>Name:</b>
name</p>
<p><b>Email:</b>
[email protected]</p>
<p><b>Message:</b>
hi</p>
=> true
Модель:
class Contact < MailForm::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :message, :validate => true
# Declare the e-mail headers. It accepts anything the mail method
# in ActionMailer accepts.
def headers
{
:subject => "My Contact Form",
:to => "[email protected]",
:from => %("#{name}" <#{email}>)
}
end
end
Контроллер:
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(contacts_params)
@contact.request = request
if @contact.deliver
redirect_to '/'
else
render :new
end
end
def contacts_params
params.require(:contact).permit(:name, :email, :message)
end
end
конфигурация/среда/разработка:
Rails.application.configure do
config.cache_classes = false
config.eager_load = false
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.active_support.deprecation = :log
config.active_record.migration_error = :page_load
config.assets.debug = true
config.assets.digest = true
config.assets.raise_runtime_errors = true
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: '[email protected]',
password: 'password',
authentication: 'plain',
enable_starttls_auto: true }
end
просмотры/new.html.erb
<%= simple_form_for @contact, :html => {:class => 'form-horizontal' } do |f| %>
<%= f.input :name, :required => true %>
<%= f.input :email, :required => true %>
<%= f.input :message, :as => :text, :required => true %>
<div class="form-actions">
<%= f.button :submit, 'Send message', :class=> "btn btn-primary" %>
</div>
<% end %>