0

I'm currently trying to send emails via the Sendgrid SMTP API. I have a "name" substitution which replaces tags in the email with recipient's names. However, I'm running into an issue where my SMTP JSON gets declined as invalid by Sendgrid.

The issue is because the names have spaces, and the Swift mailer wraps the lines here. The linebreaks end up being in the middle of names, making those strings invalid JSON leading to Sendgrid turning that email down. I'm generating the header using Sendgrid's own SMTP API PHP library but this does not do the linewrapping for you.

I'm trying to find a way to wrap my JSON in a way that doesn't make it invalid. I haven't been able to find any PHP solutions. Sendgrid themselves give a PERL regex example, which I unsuccessfully attempted to do in PHP.

I also found a Ruby solution that is promising, but my application server supports ONLY PHP. This is the Ruby solution I found.

This is the code I'm using to send the emails.

<?php
//...
$header = new Header(); // This is the sendgrid API header class
$header->setTos(array_column($subscribers, 'email'));
$header->addSubstitution('-SubscriberName-', array_column($subscribers, 'name'));
$header->addSubstitution('-SubscriberId-', array_column($subscribers, 'id'));

// $message is a SwiftMessage
$message->getHeaders()->addTextHeader(strtoupper(Header::NAME), $header->jsonString());

This is how the header value is set, according to logs. I've removed personal data but you can see how "The(\n)Shop" would cause the aforementioned invalid JSON error. This happens many times through out the name substitutions.

X-SMTPAPI: {"to":[/* emails removed, but there were no spaces so all emails were on one very long line */],"sub":{"-SubscriberName-":["Joseph","Alexandra","Michelle","Nicky","Des","Clive","Peter","Robert","Tedy","Terry","Terry","Terry","Tessa","The 
Shop","Thomas","Tim","Tim","Tina","Tina","Tobias","Tom","Tommy", //...
serenesat
  • 4,611
  • 10
  • 37
  • 53

2 Answers2

0

May be what you need is json_encode with JSON_PRETTY_PRINT as discussed here: Pretty-Printing JSON with PHP

Community
  • 1
  • 1
kangaswad
  • 765
  • 6
  • 12
  • I already attempted pretty print. It does seem to work, but then if I try to send a batch of 100 emails, I end up with ~1000 lines of JSON headers. It also runs a fair bit slower, so it's not feasible to use in production. – ShotgunToothpaste Aug 04 '15 at 11:49
-1

SendGrid has a php library that implements the SMTPAPI header: https://github.com/sendgrid/sendgrid-php

Even if you dont use it you can always see how they are solving this problem, beauty of open source.

Good luck!

kernkw
  • 9
  • 1
  • 2
  • I was already using that. The library doesn't line wrap it for you when you tell it to generate the JSON. It comes out with huge long lines. I ended up using POST requests to the web API instead of sending via SMTP. – ShotgunToothpaste Aug 22 '15 at 10:07
  • Cool, open an issue with https://github.com/sendgrid/sendgrid-php, as that seems like a bug to me – kernkw Aug 22 '15 at 14:41