Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newlines resolved as =0A in Sendgrid X-SMTPAPI header

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?

like image 381
Dave Isaacs Avatar asked Apr 13 '13 19:04

Dave Isaacs


People also ask

What happens if I send an invalid X-smtpapi header?

When you try to send an invalid X-SMTPAPI header, you will get an email with details about the invalidations. You may also see errors on your Email Activity page or in your Event Webhook data. If this happens, the email should give you the information you need to begin troubleshooting.

How many SMTP messages does SendGrid send?

Each SMTP call you make returns a response. 200 responses are usually success responses, and 400 responses are usually deferrals. SendGrid continues to retry resending 400 messages for up to 72 hours. 500 responses are hard failures that are not retried by our servers.

Why am I getting an invalid SMTP header error?

When you are sending an email via SendGrid SMTP service, you might face the error like mentioned above: Invalid SMTPAPI Header The unique_args variable must be a hash This actually happen when the X-SMTPAPI header is malformed. eg.: The sample above is invalid, as the unique_args must be an key/value paired type (associative) array

How do I Turn Off click tracking on X-smtpapi headers?

This is what SendGrid displays when the reciepients server returns a blank reason code. To turn off click tracking, add this to your X-SMTPAPI header: When you try to send an invalid X-SMTPAPI header, you will get an email with details about the invalidations. You may also see errors on your Email Activity page or in your Event Webhook data.


1 Answers

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.

like image 164
Swift Avatar answered Oct 29 '22 14:10

Swift