1

I am sending a simple mail using mailgun library when i runs the code on my development server than everything looks fine and mail is sending with attachment. But when i run same code after uploading to my cpanel the mail is sending without attachment. any help would be appreciated. Thanks

//Call the function to send an email
 $subject='subject for mail';
 $body='<b>Mail body</b>';
 $email='joshiprakash9090@gmail.com';
 //$attachment=array('@/home/unitedd5/public_html/beta/upload/VIBES.png','checkbox.htm');  
 $attachment=array('@test.png','@test1.png');  
 sendmail($email, $subject, $body,$attachment);

//MailGun function to send mail with attacment.
function sendmail($to, $subject, $body,$attachment) {
    $mg_api = 'key-mykey';
    $mg_version = 'api.mailgun.net/v2/';
    $mg_domain = "mydomain";       

    $mg_message_url = "https://" . $mg_version . $mg_domain . "/messages";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

    curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

    curl_setopt($ch, CURLOPT_USERPWD, 'api:' . $mg_api);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_POST, true);    
    curl_setopt($ch, CURLOPT_HEADER, false);

    $emailSetter=array('from' => 'Unitedcheerleading <' . 'sales@unitedcheerleading.com' . '>',
        'to' => $to,
        'subject' => $subject,
        'html' => $body,        
    );

    $i=0;
    foreach ($attachment as $attach)
    {
        $emailSetter['attachment['.$i.']']=$attach;
        $i++;

    }

    curl_setopt($ch, CURLOPT_URL, $mg_message_url);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$emailSetter);
    $result = curl_exec($ch);
    print_r($result);
    curl_close($ch);
    $res = json_decode($result, TRUE);
    print_r($res);
}
?>
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
user3446467
  • 95
  • 1
  • 13

1 Answers1

3

I believe I did not go through properly for searching the solution for this on stack overflow before posting the question apologies for this.

I just changed my code as follow. $attachment=array('@test.png','@test1.png'); with

$attachment=array('test.png','test1.png'); 

And

foreach ($attachment as $attach)
    {
        $emailSetter['attachment['.$i.']']=$attach;
        $i++;

    }

as

 foreach ($attachment as $attach)
    {
        $emailSetter['attachment['.$i.']']=curl_file_create($attach);
        $i++;

    }

And its did the job this question is look like duplicate I found the answer from

Mailgun Sent mail With attachment

The third answer did the job.

Community
  • 1
  • 1
user3446467
  • 95
  • 1
  • 13