【问题标题】:Plain text email encoding issues in Ruby on RailsRuby on Rails 中的纯文本电子邮件编码问题
【发布时间】:2015-11-06 16:43:48
【问题描述】:

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

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

示例: 我得到的是 ' ;在哪里购买表格' ;

这是我的代码:

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

联系表格示例

= 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

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

【问题讨论】:

  • 我也有同样的问题,你找到解决方法了吗?
  • 我无法让它工作。
  • 其实我发现了我的问题。它来自 inky-rb gem:stackoverflow.com/a/41634312/3700317。谢谢!

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


【解决方案1】:

尝试像这样编码:

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

【讨论】:

  • 感谢您发布此问题的答案!在 Stack Overflow 上不鼓励仅使用代码的答案,因为没有上下文的代码转储无法解释解决方案的工作方式或原因,这使得原始发布者(或任何未来的读者)难以理解其背后的逻辑。请编辑您的问题并包含对您的代码的解释,以便其他人可以从您的回答中受益。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-19
  • 2010-09-08
  • 2012-02-05
相关资源
最近更新 更多