Ruby on Rails中的纯文本电子邮件编码问题

时间:2015-08-13 15:46:32

标签: ruby-on-rails ruby-on-rails-4 character-encoding mail-form email-formats

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

出于某种原因,每个特殊角色都会像一堆胡言乱语一样到来:编码问题!

实施例: 而不是'在哪里购买表格',我得到'在哪里购买表格'

这是我的代码:

模型/ 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 个答案:

答案 0 :(得分:0)

尝试编码如:

#encoding: utf-8
class Contact < MailForm::Base
  ..
end