I am facing issue when mail having attachment sent using mailgun. If anyone has done this thing please reply. This is my code...
$mg_api = \'key-3ax6xnjp29jd
Add attachment file:
"attachment[1]" = "@$_FILES['careerDocument']['tmp_name'];filename=test.jpg".
($contentType ? ';type=' . $contentType : '' ) ;
curl_setopt($ch, CURLOPT_POSTFIELDS, ($message));
You need to change the last parameter in the following way:
attachment[1]' => '@aaa.rar
We have some samples in our documentation for this use case. Just click PHP in the top bar to switch the language. http://documentation.mailgun.net/user_manual.html#examples-sending-messages-via-http
Please don't hesitate to send us an email with any questions to support@mailgunhq.com. We are always happy to help.
I got stuck on this issue for a while and the answers here helped me, but there is something I came across which is not mentioned yet.
I had been sending my POST parameters with a blank/NULL 'cc' value, such as: $post_data['cc'] = NULL;
. This did not prevent me from sending text emails (no attachment), but it did cause problems when sending an email with an attachment. Removing the blank cc
from my array resolved part of the issue.
Additionally I had been using http_build_query
before posting my data via PHP curl and that prevented my email with attachment from being sent successfully.
Removing the empty cc
and http_build_query
resolved this for me. Maybe an uncommon case, but posting in case it is helpful for someone who has the same issue.
I realise this is old but I've just run into this problem and this page is pretty much the only info I can find on the net. So I hope this will assist someone else. After talking to MailGun support I've found the following works with the current API.
Cycles through an array of zero to n attachment files:
$attachmentsArray = array('file1.txt', 'file2.txt');
$x = 1;
foreach( $attachmentsArray as $att )
{
$msgArray["attachment[$x]"] = curl_file_create( $att );
$x ++;
}
// relevant cURL parameter, $msgArray also contains to, from, subject parameters etc.
curl_setopt($ch, CURLOPT_POSTFIELDS,$msgArray);
Thsi worked for me:
<?php
$filePath='@Wealth_AC_AMF.pdf';
$curl_post_data=array(
'from' => 'Excited User <noreply@test.com>',
'to' => 'test@gmail.com',
'subject' => 'Hello',
'text' => 'test',
'attachment[1]' => $filePath
);
$service_url = 'https://api.mailgun.net/v3/test.com/messages';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "api:key-test");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);
var_dump($response);
?>
This worked for me...
class Email {
function send($array, $file_array) {
$mgClient = new Mailgun('YOUR_MAILGUN_API_KEY');
$domain = "mg.YOUR_DOMAIN.com";
$result = $mgClient->sendMessage($domain, $array, array('attachment' => $file_array));
return $result;
}
}
$array = array(
'from' => 'From <from@from.com>',
'to' => 'To <to@to.com>',
'subject' => 'Your Subject',
'html' => '<h1>Hooray!</h1>',
);
$file_array = array('/var/www/scripts/test.csv');
echo json_encode($Email::send($array, $file_array));