Я пытаюсь отправить электронное письмо с 1-n вложениями через API Mailgun, используя cURL.Приведенный ниже код успешно отправляет электронное письмо, но вложения не включены.
Кто-нибудь может понять, почему ниже не будет работать? .. Любая помощь будет очень ценится.
// Information submitted in form
$attachments = [];
if (!empty($_FILES['attachments']['tmp_name'][0]))
{
$total = count($_FILES['attachments']['tmp_name']);
// echo $total;
for ($ct = 0; $ct < $total; $ct++)
{
$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['attachments']['name'][$ct]));
$filename = $_FILES['attachments']['name'][$ct];
if (move_uploaded_file($_FILES['attachments']['tmp_name'][$ct], $uploadfile)) {
array_push($attachments, [ $uploadfile, $filename ]);
} else {
$msg .= 'Failed to move file to ' . $uploadfile;
}
}
}
// Build message body
$messageBody = "<p><strong>Attachments submitted!</strong></p>
<p>All submitted documents are attached.</p>";
// Build email configuration
$config = array();
$config['api_key'] = $api_key;
$config['api_url'] = 'https://api.mailgun.net/v3/'.$api_domain.'/messages';
$message = array();
$message['from'] = $send_from;
$message['to'] = $send_to;
$message['subject'] = "Attachment submission";
$message['html'] = $messageBody;
$message['attachment'] = $attachments;
echo print_r($message);
// Run API call
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $config['api_url']);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "api:{$config['api_key']}");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $message);
$result = curl_exec($curl);
curl_close($curl);
echo print_r($message);
печатает следующее:
Array ( [from] => xxx [to] => xxx [subject] => Attachment submission [html] =>
Attachments submitted!
All submitted documents are attached.
[attachment] => Array ( [0] => Array ( [0] => /private/var/folders/qn/cwhh2swx3wj_qxbhzg3brcbh0000gn/T/78c3c8a74d56fdc1c74b21a1e5d15c49f2644d71AN8hnn [1] => test.jpg ) ) ) 1"{\n \"id\": \"<20180605063316.1.940D1781C7E44C03@xxx.com>\",\n \"message\": \"Queued. Thank you.\"\n}"