6

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."

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 => ['[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]']
}.to_json.gsub(',',",\n ")

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

X-SMTPAPI: {"to":["[email protected]",=0A "[email protected]",=0A "[email protected]",=0A "[email protected]",=0A "[email protected]",=0A "[email protected]"]}

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?

Edit: 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: <[email protected]>
Mime-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
X-SMTPAPI: {"to":["[email protected]",=0A "[email protected]",=0A
 "[email protected]",=0A "[email protected]",=0A "[email protected]",=0A
 "[email protected]"]}

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?

2
  • and if you puts headers["X-SMTPAPI"] just after to_json it the output is the same?
    – ted
    Apr 13, 2013 at 20:56
  • Yes, it is the same. But I noticed that there is some line wrapping being applied, but not where I inserted it. See the edit above. Apr 13, 2013 at 22:23

2 Answers 2

6

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:

Ex.

headers["X-SMTPAPI"] = JSON.generate({
  :category => "welcome_email",
  :to => ['[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]']
}, :indent => ' ')

Which would result in:

X-SMTPAPI: { "category":"welcome_email", "to":[  "[email protected]",
 "[email protected]",  "[email protected]",  "[email protected]",  
 "[email protected]",     "[email protected]"]}

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

2
  • You are correct. I had just figured that out while spelunking through the ActionMailer Mail::UnstructuredField class. Thanks! Apr 15, 2013 at 2:21
  • 1
    I'll add that Sendgrid actually requires you to use this approach (spaces vs newlines) if using Rails and ActionMailer; if you embed newlines directly Sendgrid throws an error parsing the JSON but if you use spaces the ActionMaler will put it into a Sendgrid-friendly format.
    – jpw
    Dec 29, 2014 at 18:04
0

It seems like characters in headers have to be encoded according to the rules of RFC 2047 [14].
Accodingly to ASCII table %0A states for \n

4
  • Then how can I fold long header lines, if my newline characters are being encoded automatically? Apr 13, 2013 at 23:46
  • @DaveIsaacs, i'm not sure, but you may try ...gsub(',',",\\n ")
    – ted
    Apr 14, 2013 at 7:55
  • I tried that. Doesn't work. The \\n gets resolved as two individual characters: a \ and an n. Apr 14, 2013 at 11:42
  • @DaveIsaacs, isn't it interpretated as new line later?.. bad(
    – ted
    Apr 14, 2013 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.