电子邮件不会发送联系表格?

时间:2015-07-04 18:56:56

标签: ruby-on-rails ruby forms email smtp

我正在尝试实施联系表单。我没有收到任何错误,在填写表单后,它会给出成功警报,除非邮件没有发送。我认为这与我设置SMTP的方式有关?我使用了MailForm gem,就是这样。

表单视图:

<div class="container">
<h1>Contact</h1>
 <%= form_for @contact do |f| %>
    <%= f.text_field  :name, placeholder: "Name", :required => true %><br>
    <%= f.text_field  :email, placeholder: "Email", :required => true %><br>
    <%= f.text_area :message, placeholder: "Message", :as => :text, :required => true %>
    <div class= "hidden">
      <%= f.text_field  :nickname, :hint => 'Leave this field blank!' %>
    </div>
    <div>
      </br>
      <%= f.submit 'Send', :class=> "btn btn-primary" %>
    </div>
  <% end %>

  </div>

控制器:

class ContactsController < ApplicationController
  def new
    @contact = Contact.new
  end

  def create
  @contact = Contact.new(params[:contact])
    @contact.request = request
    if @contact.deliver
      flash.now[:notice] = 'Thank you for your message. I will get back to you shortly!'
    else
      flash.now[:error] = 'Cannot send message.'
      render :new
    end
  end

  private

 def contact_params
   params.require(:contact).permit(:name, :email, :comments)
 end

  end

型号:

class Contact < MailForm::Base
  attribute :name,      :validate => true
  attribute :email,     :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
  attribute :message
  attribute :nickname,  :captcha  => true

  # Declare the e-mail headers. It accepts anything the mail method
  # in ActionMailer accepts.
  def headers
    {
      :subject => "Enquiry",
      :to => "ashleighalmeida@gmail.com",
      :from => %("#{name}" <#{email}>)
    }
  end
end

配置&gt;环境&gt; development.rb:

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
  :user_name => '3878643a38ed2536d',
  :password => '1e4c3e5ef5e1ca',
  :address => 'mailtrap.io',
  :domain => 'mailtrap.io',
  :port => '2525',
  :authentication => :cram_md5
}

config&gt;的environment.rb

# Load the Rails application.
require File.expand_path('../application', __FILE__)

# Initialize the Rails application.
Rails.application.initialize!

ActionMailer::Base.delivery_method = :smtp

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您是否在托管服务器上拥有此功能?如果不是这就是原因,通常情况下,除非您进行设置,否则您无法从您的网站发送电子邮件。我无法想出恰当的方式来表达这一点,但我确信你明白了。如果有人会改进我说的话。