2015-07-04 105 views
0

我正在尝试实现联系表单。我没有收到任何错误,并且在我填写完表单之后,它会给出成功警报,除了邮件没有发送。我认为这与我设置SMTP的方式有关?我使用MailForm宝石,就是这样。电子邮件不会发送联系表格?

窗体视图:

<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 => "[email protected]", 
     :from => %("#{name}" <#{email}>) 
    } 
    end 
end 

配置>环境> 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 
} 

配置>的environment.rb

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

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

ActionMailer::Base.delivery_method = :smtp 

谢谢您的帮助!

+0

您可以像[letter_opener](https://github.com/ryanb/letter_opener)那样使用gem来捕获开发过程中的电子邮件。 –

回答

1

您是否在托管服务器上安装了该软件?如果不是这就是为什么,通常情况下,除非你设置它,否则你不能从你的网站发送电子邮件。我不能想出正确的方式来说话,但我确定你明白了。如果有人会改善我说的话。

+0

哦好吧,是的,我现在在当地工作。希望那就是问题了!谢谢! – icecreamrabbit

+0

是的,没问题。几年前我遇到了同样的问题,哈哈 – Zenithian

相关问题