MailForm 不发送电子邮件
MailForm is not sending email
我正在尝试为网站设置联系表单。在控制台中似乎一切正常,但我没有收到任何电子邮件。有人能告诉我我做错了什么吗?我检查了其他答案,但它们没有解决我的问题。
控制台:
Loading development environment (Rails 4.2.0)
irb(main):001:0> c = Contact.new(:name => "name", :email => "email@email.com", :message => "hi")
=> #<Contact:0x007ffdbbc16360 @name="name", @email="email@email.com", @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 myemail@gmail.com (7.3ms)
Date: Tue, 17 Feb 2015 10:44:49 +0000
From: name <email@email.com>
To: myemail@gmail.com
Message-ID: <54e31ba17450_c483ffedd865be84842c@Users-iMac.local.mail>
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@email.com</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 => "myemail@gmail.com",
: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
config/enviroment/development:
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: 'myemail@gmail.com',
password: 'password',
authentication: 'plain',
enable_starttls_auto: true }
end
views/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 %>
首先您应该删除警告,因为现在应该使用 ActiveQueue 发送电子邮件。
那么您收不到电子邮件,可能是由于配置错误。由于您尚未发布您的配置,我建议您完成 guides 并设置您的邮件开发配置文件。
我的快速路径是 use Gmail to send your emails:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.com',
user_name: '<username>',
password: '<password>',
authentication: 'plain',
enable_starttls_auto: true }
我正在尝试为网站设置联系表单。在控制台中似乎一切正常,但我没有收到任何电子邮件。有人能告诉我我做错了什么吗?我检查了其他答案,但它们没有解决我的问题。
控制台:
Loading development environment (Rails 4.2.0)
irb(main):001:0> c = Contact.new(:name => "name", :email => "email@email.com", :message => "hi")
=> #<Contact:0x007ffdbbc16360 @name="name", @email="email@email.com", @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 myemail@gmail.com (7.3ms)
Date: Tue, 17 Feb 2015 10:44:49 +0000
From: name <email@email.com>
To: myemail@gmail.com
Message-ID: <54e31ba17450_c483ffedd865be84842c@Users-iMac.local.mail>
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@email.com</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 => "myemail@gmail.com",
: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
config/enviroment/development:
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: 'myemail@gmail.com',
password: 'password',
authentication: 'plain',
enable_starttls_auto: true }
end
views/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 %>
首先您应该删除警告,因为现在应该使用 ActiveQueue 发送电子邮件。 那么您收不到电子邮件,可能是由于配置错误。由于您尚未发布您的配置,我建议您完成 guides 并设置您的邮件开发配置文件。
我的快速路径是 use Gmail to send your emails:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.com',
user_name: '<username>',
password: '<password>',
authentication: 'plain',
enable_starttls_auto: true }