2015-07-04 109 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

相關問題