dragon8997474 2014-12-09 09:24
浏览 165

如何使用curl php发送电子邮件附件

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

I have to attach files which is not multipart/form data. I mean I have absolute paths of files, so how can I send attachments through Curl php?

I saw mailgun document. I found this:

File attachment. You can post multiple attachment values. Important: You must use multipart/form-data encoding when sending attachments.

But I have only absolute paths not multipart/form data. So how can I attach now?

Adding headers as:

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

Here is my input array sending through 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 curl request

$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 mail inbox but i am not receiving attachments. looking at the logs also it says attachments empty

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler