2
전자 메일을 보내지 만 양식에 오류가 없는지 확인하지 않는 rails actionMailer 문의 양식이 있습니다. 잘못된 이메일을 보내면 올바른 이메일 인 이 모두 전송됩니다. 양식이 비어 있으면 이메일이 전송되지 않으며 오류 메시지도 나타나지 않지만 이메일을 보내면 알림 알리미가 작동합니다. 보냈습니다.ActionMailer 및 유효성 검사
도움을 주시면 감사하겠습니다.
모델/support.rb
class Support
include ActiveModel::Validations
validates_presence_of :email, :sender_name, :support_type, :content
# to deal with form, you must have an id attribute
attr_accessor :id, :email, :sender_name, :support_type, :content
def initialize(attributes = {})
attributes.each do |key, value|
self.send("#{key}=", value)
end
@attributes = attributes
end
def read_attribute_for_validation(key)
@attributes[key]
end
def to_key
end
def save
if self.valid?
Notifier.support_notification(self).deliver!
return true
end
return false
end
end
컨트롤러 */* supports_controller.rb
class SupportsController < ApplicationController
def new
# id is required to deal with form
@support = Support.new(:id => 1)
end
def create
@support = Support.new(params[:support])
if @support.save
redirect_to('/contact', :notice => "Your message was successfully sent.")
else
flash[:alert] = "You must fill all fields."
render 'new'
end
end
end
* 전경/지원/form_.html.erb *
<% form_for @support, :url => { :action => "create" }, :html => { :method => :post } do |f| %>
<p>
<%= f.label "Name" %>
</p>
<p>
<%= f.text_field :sender_name, "size" => 37 %>
</p>
<p>
<%= f.label "Email" %>
</p>
<p>
<%= f.text_field :email, "size" => 37 %><br /><br />
</p>
<p>
<%= f.label "Subject" %>
</p>
<p>
<%= f.select :support_type, options_for_select(["Hire", "General", "Collaboration"]) %>
</p>
<p>
<%= f.label "Details" %>
</p>
<p>
<%= f.text_area :content, "rows" => 3, "cols" => 27 %>
</p>
<p><br />
<%= f.submit "Submit" %>
</p>
<% end %>
초기화 프로그램/mailer.rb
# config/initializers/mailer.rb
ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.perform_deliveries = true #default value
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.sendmail_settings = {
:tls => true,
:address => 'smtp.test.com',
:port => 587,
:domain => 'test.com',
:user_name => '[email protected]',
:password => '#',
:authentication => 'login',
:openssl_verify_mode=>nil,
:enable_starttls_auto => true
}
나는 또한이 같은 이메일 양식에 대한 매우 조심해야합니다. 그들은 봇과 스패머 등에 대한 매력입니다. – PeppyHeppy