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 高德地图点聚合中Marker的位置无法实时更新
    • ¥15 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办