我正在嘗試實現聯繫表單。我沒有收到任何錯誤,並且在我填寫完表單之後,它會給出成功警報,除了郵件沒有發送。我認爲這與我設置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
謝謝您的幫助!
您可以像[letter_opener](https://github.com/ryanb/letter_opener)那樣使用gem來捕獲開發過程中的電子郵件。 –