Recently I've been working on a on-boarding application for an agency client that involved some fallback processes if their CRM write failed. The client requested we send them an email with the form data attached as JSON, so they could manually process if required.
We are using PHPMailer as a more comprehensive mailing solution rather than PHP's
mail() function. Let's say our form data looks like this:
$data = ['foo' => 'bar'];
What we're after is to set this variable as a file. we can do this using PHP mailers AddStringAttachement function which looks like this:
Applying this to our example could be the following:
$contents = json_encode($data, JSON_PRETTY_PRINT);
PHP has had the
JSON_PRETTY_PRINT option since v5.4.0.