douqian1975 2019-02-05 12:11
浏览 221

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
    • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
    • ¥15 onvif+openssl,vs2022编译openssl64
    • ¥15 iOS 自定义输入法-第三方输入法
    • ¥15 很想要一个很好的答案或提示
    • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
    • ¥15 怀疑手机被监控,请问怎么解决和防止
    • ¥15 Qt下使用tcp获取数据的详细操作