Проблемы с кодировкой электронных писем в Ruby on Rails

У меня есть приложение Rails со многими контактными формами. Пользователь выбирает тему, получает форму, заполняет пробелы и при отправке электронное письмо отправляется в виде простого текста на адрес по умолчанию. Я использую Rails 4.2.1 и gem 'mail_form 1.5.1

По какой-то причине каждый специальный символ прибывает как куча тарабарщины: проблемы с кодированием!

Пример.Вместо "Где купить форму" я получаю форму "Где купить" & # 39;

Это мой код:

модель /contact.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

среда /production.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

contacts_controller.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

Какие-нибудь мысли!? Заранее спасибо!

1 ответ

Попробуйте кодировку как:

#encoding: utf-8
class Contact < MailForm::Base
  ..
end
Другие вопросы по тегам