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 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题