【问题标题】:MailForm is not sending emailMailForm 不发送电子邮件
【发布时间】:2015-02-17 11:08:14
【问题描述】:

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

控制台:

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

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 %>

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 2015-03-09
      • 2014-08-17
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多