ยท 1 min read
Sending a variable as an attachment with PHP Mailer
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:
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:
PHP has had the JSON_PRETTY_PRINT
option since v5.4.0.