1

我有一个带有许多联系表格的 Rails 应用程序。用户选择主题,获取表格,填写空白,并在提交后以纯文本形式将电子邮件发送到默认地址。我正在使用 Rails 4.2.1 和 gem 'mail_form 1.5.1

出于某种原因,每个特殊字符都像一堆乱码一样出现:编码问题!

示例: 我得到的不是“在哪里购买表格”,而是 ' ;在哪里购买表格' ;

这是我的代码:

型号/联系人.rb

class Contact < MailForm::Base
  attribute :mail_subject
  attribute :first_name,             validate: true
  attribute :last_name,              validate: true
  attribute :company
  attribute :email,                  validate: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i

  def headers
    {
      mime_version: "1.0\n",
      content_type: "text/plain; charset=iso-8859-1\n",
      # content_type: "text/plain; charset=utf-8" => also tried
      subject:      %(#{mail_subject}),
      to:           "xxxx@xxxxxxxxxx.com",
      from:         "yyyy@yyyyyyyyyy.com"
    }
  end
end

环境/生产.rb

Rails.application.configure do

  # xxxxx more config's

  config.action_mailer.default_url_options = { host: 'xyxyxyxyx.herokuapp.com' }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.default :charset => "utf-8"

  config.action_mailer.smtp_settings = {
    address: "smtp.office365.com",
    port: 587,
    domain: "yxyxyxyxyx.com",
    authentication: :login,
    enable_starttls_auto: true,
    user_name: ENV["OFFICE_USERNAME"],
    password: ENV["OFFICE_PASSWORD"],
    encryption: "TLS w/STARTTLS"
  }

  # xxxxx more config's

end

意见/mail_form/contact.erb

<%= message.subject %>

<% @resource.mail_form_attributes.each do |attribute, value|%>
  <% if value.blank? && attribute != "company" %>
    <% next %>
  <% elsif attribute == "mail_subject" %>
    <% next %>
  <% end %> 

  <%= "#{@resource.class.human_attribute_name(attribute)}: #{value}" %>
<% end %>

联系表格示例

= form_for @contact do |f|
  .hide
    = f.text_field     :nickname,     hint: 'Leave this field empty!'
    = f.hidden_field   :mail_subject, value: @the_subject
  .phone-12.tablet-6.columns
    = f.label          :first_name,   'First Name: *'
    = f.text_field     :first_name,   required: true
  .phone-12.tablet-6.columns
    = f.label          :last_name,    'Last Name: *'
    = f.text_field     :last_name,    required: true
  .phone-12.columns
    = f.label          :company,      'Company:'
    = f.text_field     :company
  .phone-12.columns
    = f.label          :email,        'Email: *'
    = f.email_field    :email,        required: true
  .phone-12.columns
    = f.label          :message, "Message: *"      
    = f.text_area      :message, required: true, rows: 4, as: :text       
  .phone-12.columns.contact-submit
    = f.submit "Send Message"                                                                                         
  .phone-12.columns
    %small.contact-required * indicates required fields

联系人控制器.rb

class ContactsController < ApplicationController

  def create
    @contact         = Contact.new(params[:contact])
    @contact.request = request
    if @contact.deliver
      @thank         = "Thank you for your message!"
      @message       = "We have received your inquiry and we'll be in touch shortly."
    else
      flash[:alert] = "Make sure you are filling the required fields and using the correct formats"
      redirect_to :back
    end
  end

  def contact_page
  end

  def example_email_form
    @contact = Contact.new
    @the_subject = "Where to Buy Form"
  end
end

有什么想法吗!?提前致谢!

4

1 回答 1

0

尝试像这样编码:

#encoding: utf-8
class Contact < MailForm::Base
  ..
end
于 2015-08-13T16:12:31.143 回答