I’m having trouble getting file attachments to work when sending emails via the Mailgun API. The email gets delivered successfully but the attached file never shows up. When I check the Mailgun dashboard logs, I can see that the attachments array is completely empty.
Here’s my current code setup:
$document_path = 'uploads/report.docx';
$email_data = array(
'from' => 'Support Team <[email protected]>',
'to' => '[email protected]',
'subject' => 'Monthly Report',
'text' => 'Please find the attached report.',
'attachment[1]' => '@' . $document_path
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.mailgun.net/v3/mysite.com/messages');
curl_setopt($curl, CURLOPT_USERPWD, 'api:key-MySecretApiKey');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($curl, CURLOPT_POSTFIELDS, $email_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
The API response looks normal:
{
"id": "<[email protected]>",
"message": "Queued. Thank you."
}
But in the logs, no attachments are showing:
"message": {
"headers": {
"to": "[email protected]",
"message-id": "[email protected]",
"from": "Support Team <[email protected]>",
"subject": "Monthly Report"
},
"attachments": [],
"size": 349
}
The file exists and has proper read permissions. What am I missing here?