Adding Attachments To Email Using the MailGun.Com PHP API

When sending messages via mailgun I really struggled to add an attachment using the obvious method:

$mg = new Mailgun('mailgun-api-key');
$msg = $mg->MessageBuilder();
$msg->setFromAddress($from);
$msg->setSubject($subject);
$msg->setTextBody($textBody);
$msg->addAttachment('/var/www/somefile.php');
$mg->post("{$domain}/messages", $msg->getMessage());

The solution was to do this….

$mg = new Mailgun('mailgun-api-key');
$msg = $mg->MessageBuilder();
$msg->setFromAddress($from);
$msg->setSubject($subject);
$msg->setTextBody($textBody);
$files['attachment'] = array();
$files['attachment'][] = '/var/www/somefile.php';
$mg->post("{$domain}/messages", $msg->getMessage(), $files);

Leave a Reply

Your email address will not be published. Required fields are marked *


*