Sendgrid X-SMTPAPI 标头中的换行符解析为 =0A [英] Newlines resolved as =0A in Sendgrid X-SMTPAPI header

查看:48
本文介绍了Sendgrid X-SMTPAPI 标头中的换行符解析为 =0A的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Sendgrid 将电子邮件发送到邮件列表,使用 X-SMTPAPI 标头指定多个收件人.来自 Sendgrid 文档必须包装标题以保持行长度低于 72."

I am using Sendgrid to send email to a mailing list, using the X-SMTPAPI header to specify the multiple recipients. From the Sendgrid documentation "Headers must be wrapped to keep the line length under 72."

我使用 ActionMailer 发送电子邮件,并使用 headers 方法设置 X-SMTPAPI 标头.为了保持行少于 72 个字符,我尝试用逗号+换行符+空格替换每个逗号.例如,

I am using the ActionMailer to send emails, and setting the X-SMTPAPI header using the headers method. To keep lines less than 72 characters, I have tried replacing each comma with a comma+newline+space. For example,

headers["X-SMTPAPI"] = {
        :to => ['user1@example.com','user2@example.com','user3@example.com','user4@example.com','user5@example.com','user6@example.com']
}.to_json.gsub(',',",\n ")

我没有在标题中获取换行符,而是获取以下内容(来自日志文件)

Instead of getting newlines in my header, I am getting the following (from the log file)

X-SMTPAPI: {"to":["user1@example.com",=0A "user2@example.com",=0A "user3@example.com",=0A "user4@example.com",=0A "user5@example.com",=0A "user6@example.com"]}

请注意,\n 字符将被替换为 =0A.这个序列被 Sendgrid 服务器拒绝为无效.

Note that the \n characters are being replaced with =0A. This sequence is rejected as invalid by the Sendgrid server.

有什么想法可以让正确的换行符进入标题吗?

Any ideas what I can do to get the proper newlines into the header?

我尝试添加一个放置标题"以查看标题中设置的内容.然后就是我发现的

I tried adding a "puts headers" to see what is being set in the headers. Then is what I found

Date: Sat, 13 Apr 2013 18:21:36 -0400
Message-ID: <5169da701cd26_5343fe1776afc50749b4@saunders.mail>
Mime-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
X-SMTPAPI: {"to":["user1@example.com",=0A "user2@example.com",=0A
 "user3@example.com",=0A "user4@example.com",=0A "user5@example.com",=0A
 "user6@example.com"]}

注意我添加的换行符仍然显示为=0A".但似乎有些东西会自行添加包装.这种换行是否是自动的,是否足以防止我的标题行长度超出要求?

Note the newlines I am adding are still showing up as "=0A". But something appears to be adding wrapping on its own. Is this wrapping automatic, and sufficient to keep my header line length from exceeding the requirements?

推荐答案

ActionMailer 实际上会为你处理折叠和编码行,如果你给它适当的间距来这样做.你应该使用 JSON.generate 给它间距:

ActionMailer actually will handle folding and encoding the lines for you if you give it the proper spacing to do so. You should use JSON.generate to give it the spacing:

例如

headers["X-SMTPAPI"] = JSON.generate({
  :category => "welcome_email",
  :to => ['user1@example.com','user2@example.com','user3@example.com','user4@example.com','user5@example.com','user6@example.com']
}, :indent => ' ')

这将导致:

X-SMTPAPI: { "category":"welcome_email", "to":[  "user1@example.com",
 "user2@example.com",  "user3@example.com",  "user4@example.com",  
 "user5@example.com",     "user6@example.com"]}

如您所见,当 ActionMailer 遇到空格时,它会为您包装 - 不需要通常的 \r\n.

As you can see, when ActionMailer encounters whitespace, it will wrap things for you - no need for the usual \r\n.

这篇关于Sendgrid X-SMTPAPI 标头中的换行符解析为 =0A的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆