How to send email attachments using curl php

I am using mailgun api to send emails. My php version is 5.3

I need to attach files that are not multipart / form data. I mean I have absolute file paths, so how can I send attachments via Curl php?

I saw an email document. I found this:

Attaching files. You can post multiple binding values. Important: when sending attachments, you must use the multipart / form-data encoding.

But I only have absolute paths, not multipart/form data

. So how can I attach now?

Adding headers like:

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));

      

Here is my input array being sent via the mailgun API:

Array ([from] => xyz

 [to] => abc@gmail.com

[subject] => ryreyreyre

[text] => yreyreyreyreyre<br />&nbsp;<br />Sincerely,<br />xyz
[html] => yreyreyreyreyre<br />&nbsp;<br />Sincerely,<br />xyz
[attachment] => Array
    (
        [0] => @/var/www/vhosts/download/attachment/1418034032618discover.png
        [1] => @/var/www/vhosts/download/attachment/1418034032395master.png
        [2] => @/var/www/vhosts/download/attachment/1418034032208visa.png
    )

      

)

Here is my request for curls

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, 'api:'.MAILGUN_APIKEY);
    if(!empty($postArr['attachments']))
    {
        curl_setopt ($ch, CURLOPT_VERBOSE, 0);
         curl_setopt ($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

    curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/'.MAIL_VIA_DOMAIN.'/messages');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postArr);

   $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $result = curl_exec($ch);
    curl_close($ch);

      

mail is being sent to my mailbox, but I am not receiving attachments. looking at the magazines, he also says that the attachments are empty

+3


source to share





All Articles