【问题标题】:How to change the mailer Content-Transfer-Encoding settings in Rails?如何更改 Rails 中的邮件内容传输编码设置?
【发布时间】:2012-07-28 01:09:45
【问题描述】:

“内容传输编码”设置默认设置为“7 位”。 邮件服务器 Postfix 将电子邮件标题分解为 1000 个字符,这意味着如果您的电子邮件很长(例如使用 HTML),您最终会在文本或链接中间有空格。 (有关更多信息,请参阅此线程:http://tech.groups.yahoo.com/group/postfix-users/message/273296

按照 Rails ActionMailer 文档 (http://api.rubyonrails.org/classes/ActionMailer/Base.html),将以下代码添加到我的应用程序文件中应该可以,但它不起作用:

ActionMailer::Base.default 'Content-Transfer-Encoding' => 'quoted-printable'

我仍然以默认值结束:

Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_50166adf1e043_1b9810829142282d";
 charset=UTF-8
Content-Transfer-Encoding: 7bit

我的电子邮件是这样的:

def new_registered_user(user_id) 
    @user = User.find(user_id)

    set_locale @user.locale

    mail(
      :subject => i18n_subject,
      :to => @user.email_with_name
    ) do |format|
      format.text { render :layout => 'text_email' }
      format.html
    end
  end

知道我还应该改变什么吗?

【问题讨论】:

    标签: ruby-on-rails actionmailer postfix-mta


    【解决方案1】:

    我发现在 Mail 对象上设置(未记录的)transport_encoding 是可行的:

    m = mail(...)
    m.transport_encoding = "quoted-printable"
    m.deliver
    

    我无法获得通过 ActionMailer 设置 Content-Transfer-Encoding 工作的记录方法。

    我的环境:rails (3.1), mail (~> 2.3.3)

    【讨论】:

    • 仅供参考,电子邮件行长度限制为 998 个字符。见stackoverflow.com/questions/1592291/…
    • 非常感谢,所以除了为长邮件添加断线(\r\n)之外别无选择
    • 确实如此。每 998 个字符必须有一个 CRLF。也就是说,根据您的内容编码,您可以避免将 CRLF 放入内容本身。
    • 非常感谢!问答..这是一个多么棘手的问题!而不是m.transport_encoding = "quoted-printable",我使用m.transport_encoding = "base64",因为我们必须用外国字符发送邮件,根据维基百科:“Base64 ...是二进制格式或非拉丁语言文本的更明智选择。”来自en.wikipedia.org/wiki/Quoted-printable
    • 附注我很幸运能找到这个问题。我的文本和链接中存在同样的空格问题,但不知道必须更改 content_transfer_encoding。为了其他人的利益,在标题中添加“Rails 电子邮件文本和链接中的随机空格”或其他内容可能会很好。或者仅此评论可能有助于搜索结果。
    【解决方案2】:

    如果您使用自定义邮件程序类,您可以将其设置为邮件程序类中的默认选项,如documentation of ActionMailer::Base 中所述

    class Notifier < ApplicationMailer
      default 'Content-Transfer-Encoding' => '7bit'
    end
    

    【讨论】:

      【解决方案3】:

      我最近也遇到了这个问题。 Rails 默认为 quoted-printable,但这导致我在 webmail 客户端中出现渲染问题。新行渲染为=0D,webmail 客户端渲染不正确。

      我对此的解决方案是仅更改 html 部分的内容编码,因为我还发送了单独的文本部分并且不想更改其编码。 html 方法采用 content_transfer_encoding 参数,避免了对临时变量的需要:

      mail do |format|
        format.text { ... }
        format.html(content_transfer_encoding: '7bit') { ... }
      end
      

      使用default 'Content-Transfer-Encoding' ... 的文档引用但仅在不直接调用mail 方法时才有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-06
        • 1970-01-01
        • 2020-12-18
        • 1970-01-01
        • 2011-06-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多