1

I have a Rails app with many contact forms. A user chooses the topic, gets a form, fills the blanks and upon submission an email gets sent in plain text to a default address. I am using Rails 4.2.1 and gem 'mail_form 1.5.1

For some reason, every special character is arriving like a bunch of gibberish: Encoding issues!

Example: Instead of 'Where to Buy Form', I am getting &#39 ;Where to Buy Form&#39 ;

This is my code:

model/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

environments/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

views/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 %>

contact form example

= 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

Any thoughts!? Thanks in advance!

drjorgepolanco
  • 7,479
  • 5
  • 46
  • 47

1 Answers1

0

try to encoding like:

#encoding: utf-8
class Contact < MailForm::Base
  ..
end
un_gars_la_cour
  • 259
  • 1
  • 4
  • Thank you for posting an answer to this question! Code-only answers are discouraged on Stack Overflow, because a code dump with no context doesn't explain how or why the solution will work, making it difficult for the original poster (or any future readers) to understand the logic behind it. Please, edit your question and include an explanation of your code so that others can benefit from your answer. Thanks! – Maximillian Laumeister Aug 14 '15 at 00:53