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

我使用mailgun api发送电子邮件。 我的php版本是5.3 </ p>

我必须附加不是multipart / form数据的文件。 我的意思是我有绝对的文件路径,所以如何通过Curl php发送附件?</ p>

我看到了mailgun文件。 我发现了这个:</ p>


文件附件。 您可以发布多个附件值。 重要提示:发送附件时必须使用multipart / form-data编码。</ p>
</ blockquote>

但我只有绝对路径 multipart / form data </ code> 。 那么我现在如何附加?</ p>

添加标题为:</ p>

  curl_setopt($ ch,CURLOPT_HTTPHEADER,array(“Content-type:  multipart / form-data“)); 
</ code> </ pre>

这是我通过mailgun API发送的输入数组:</ p>


< p>数组(
[from] =&gt; xyz </ p>

  [to] =&gt; abc@gmail.com 

[subject] =&gt; ryreyreyre \ n
[text] =&gt; yreyreyreyreyre&lt; br /&gt;&amp; nbsp;&lt; br /&gt;真诚地,&lt; br /&gt; xyz
[html] =&gt; yreyreyreyreyre&lt; br /&gt;&amp; nbsp;&lt; br /&gt;真诚地,&lt; br /&gt; xyz
[attachment] =&gt;数组

[0] =&gt; @ / var / www / vhosts / download / attachment / 1418034032618discover .png
[1] =&gt; @ / var / www / vhosts / download / attachment / 1418034032395master.png
[2] =&gt; @ / var / www / vhosts / download / attachment / 1418034032208visa.png

</ code> </ pre>

)</ p>
</ blockquote>

这是我的卷曲请求</ p>

< pre> $ ch = curl_init();
curl_setopt($ ch,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
curl_setop t($ 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);
</ code> < / pre>

邮件正在发送到我的邮件收件箱,但我没有收到附件。 看着日志也说附件空了</ p>
</ div>

展开原文

原文

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

donglu3184
donglu3184 我注意到如果你没有明确地设置multipart/form-data,那么它可以工作。当CURLOPT_POSTFIELDS的值为数组时,Curl会自动设置multipart标头。
4 年多之前 回复
douzhao9608
douzhao9608 你是怎么解决的?我正在处理同样的问题并得到错误:502-服务器错误-Mailgun结束时出现问题。
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐