MailForm不发送电子邮件

时间:2015-02-17 11:08:14

标签: ruby-on-rails gem mail-form

我正在尝试为网站设置联系表单。一切似乎在控制台中工作正常,但我没有收到任何电子邮件。有人能告诉我我做错了什么吗?我检查了其他答案,但他们没有解决我的问题。

控制台:

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

配置/环境/发育

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

视图/ 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 %>

1 个答案:

答案 0 :(得分:0)

首先你应该删除警告,因为现在应该使用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  }