Mailgun API - 发送多个附件

In the past, I've managed to use Mailgun to send ONE attachment. But I now have a client requirement to send more than one file through Mailgun as an attachment.

I am using the attachment[1] as an array and passing it through. The problem I have is, It's not sending the message and also not particularly providing me with any error messages or response messages.

I am using PHP and CURL to send the Email.

My PHP code is :

$curl_post_data = array
    (
        'from'    => $from,
        'to'      => $recipient,
        'subject' => $subject,
        'html'    => $message
    );

    // Deal with the file(s)...
    $x = 1;
    foreach( $files as $file )
    {
        $curl_post_data = array(
            "attachment[$x]" => curl_file_create( @$file['tempname'], $file['filetype'], $file['filename'])
        );
        $x ++;
    }

    echo "<pre>";
    print_r($curl_post_data);
    echo "</pre>";

    $service_url = 'https://api.mailgun.net/v3/sendmsg/messages';
    $curl = curl_init($service_url);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, "api:key-12322222");

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
    curl_setopt($curl, CURLOPT_POST, true);

    curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

    $curl_response = curl_exec($curl);
    $response = json_decode($curl_response);
    curl_close($curl);

    var_dump($curl_response);

The files array that gets to this function is as follows :

array(1) {
  [0]=>
  array(4) {
    ["filename"]=>
    string(8) "Paul.png"
    ["filetype"]=>
    string(9) "image/png"
    ["filepath"]=>
    string(14) "files/Paul.png"
    ["tempname"]=>
    string(24) "C:\xampp\tmp\php975D.tmp"
  }
}

Can anyone see anything in particular that may be causing this non sending issue?

Also worth noting, I'm working locally. If that's a possible issue too?

Thank You

douji5397
douji5397 好的,谢谢你让我知道。我能够解决我的问题,包括多个附件。和你的一样,我看到的一个看起来不正确的是@$file['tempname']。不过,压缩听起来也是个好主意。
一年多之前 回复
dp20011
dp20011 嘿@LawrenceJohnson如果它对你有帮助,我解决了我的问题,将所有文件添加到zip然后发送该单个文件。
一年多之前 回复
dsdapobp26141
dsdapobp26141 我目前也在发送附件时遇到问题。stackoverflow.com/questions/54678804/...
一年多之前 回复
duansai1314
duansai1314 快速且容易尝试的方法是删除CURLOPT_HTTPHEADER参数,因为根据stackoverflow.com/questions/34965313/...使用curl_file_create时,cURL配置了multipart/form-data。
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐