I’m trying to send emails with file attachments through Mailgun but running into problems. The emails are delivered successfully but the attachments are missing. When I try to include a file path, it just shows the path as text instead of attaching the actual file.
Here’s my current implementation:
$api_key = 'key-7bx9ynkm41hf8gdt6hc485thwkytrel2';
$api_version = 'api.mailgun.net/v3/';
$domain_name = "testing.mailgun.org";
$sender_email = "[email protected]";
$response_email = "[email protected]";
$message_endpoint = "https://".$api_version.$domain_name."/messages";
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_MAXREDIRS, 5);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_USERPWD, 'api:' . $api_key);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_URL, $message_endpoint);
$post_data = array(
'from' => 'TestSender <' . '[email protected]' . '>',
'to' => '[email protected]',
'h:Reply-To' => ' <' . $response_email . '>',
'subject' => 'Test Message '.date('Y-m-d'),
'html' => 'This is a test message with attachment',
'attachment' => array('document.pdf')
);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$response = curl_exec($curl);
curl_close($curl);
$output = json_decode($response, true);
var_dump($output);
I’ve double checked my Mailgun credentials and they work fine for regular emails. What’s the correct way to attach files when sending through their API?