电子邮件不会发送联系表格?
Email won't send for contact form?
我正在尝试实现一个联系表。我没有收到任何错误,在我填写表格后,它会发出成功警报,只是消息没有发送。我认为这与我设置 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
配置 > 环境 > 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
感谢您的帮助!
你在托管服务器上有这个吗?如果不是,这就是为什么,大多数情况下,除非您进行设置,否则您无法从您的网站发送电子邮件。我想不出正确的表达方式,但我相信你明白了。如果有人能改进我说的话。
我正在尝试实现一个联系表。我没有收到任何错误,在我填写表格后,它会发出成功警报,只是消息没有发送。我认为这与我设置 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
配置 > 环境 > 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
感谢您的帮助!
你在托管服务器上有这个吗?如果不是,这就是为什么,大多数情况下,除非您进行设置,否则您无法从您的网站发送电子邮件。我想不出正确的表达方式,但我相信你明白了。如果有人能改进我说的话。