I am trying to send a mail with attachments with mailgun. The mail itself is fine, but it is missing the attachment. Also in the mailgun log it shows up fine, but the attachment array is empty.
I replaced my credentials with example.com. The file is placed in a subdirectory and is readable.
$filename = 'directory/example.pdf';
$parameters = array('from' => 'Example <[email protected]>',
'to' => '[email protected]',
'subject' => 'Subject',
'text' => 'This is just a test.',
'attachment[1]' => '@' . $filename);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/mail.example.com/messages');
curl_setopt($ch, CURLOPT_USERPWD, 'api:key-ThisIsJustARandomString');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
I don't get an error, this is the $response
:
string(103) "{
"id": "<[email protected]>",
"message": "Queued. Thank you."
}"
Within the mailgun logs no attachments are listed:
"message": {
"headers": {
"to": "[email protected]",
"message-id": "[email protected]",
"from": "Example <[email protected]>",
"subject": "Subject"
},
"attachments": [],
"size": 349
},
According to all documentation I found this would be the correct solution, but it is not working.
Thanks in advance for all replies.
Change your first code to:
$filename = 'directory/example.pdf';
$parameters = array('from' => 'Example <[email protected]>',
'to' => '[email protected]',
'subject' => 'Subject',
'text' => 'This is just a test.',
'attachment[1]' => curl_file_create($filename, 'application/pdf', 'example.pdf'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/mail.example.com/messages');
curl_setopt($ch, CURLOPT_USERPWD, 'api:key-ThisIsJustARandomString');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
I changed '@'.$filename
to curl_file_create($filename, 'application/pdf', 'example.pdf')
.
See documentation curl_file_create and check the notes for PHP < 5.5.