У меня есть форма, которая успешно отправляет загруженное изображение в виде вложения через php, но я не могу получить его для отправки нескольких вложений.Отправить несколько вложений с php
HTML-:
<input id="file-3" type="file" name="image" multiple="true" required>
РНР, который прикрепляет один файл:
if (isset($email) && isset($name)) {
$to = "[email protected]"; /* <= Change this Email ID to yours */
$subject = "$name sent you a message"; /* <= Change the Subject If you want */
$random_hash = md5(date('r', time()));
$headers = "From: $email\r\nReply-To: $email";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = null;
if (isset($_FILES["photo"])) {
$attachment = chunk_split(base64_encode(file_get_contents($_FILES["photo"]["tmp_name"])));
}
$message = "--PHP-mixed-" . $random_hash . "\r\n";
$message .= "Content-Type: multipart/alternative; boundary=\"PHP-alt-" . $random_hash . "\"\r\n\r\n";
$message .= "--PHP-alt-" . $random_hash . "\r\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\n";
$message .= "Name: " . $name . "\r\nEmail: " . $email . "Message: " . $textarea . "\r\n\r\n";
$message .= "--PHP-alt-" . $random_hash . "\r\n";
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\t\nContent-Transfer-Encoding: 7bit\r\n\r\n";
$message .= "";
if ($attachment != null) {
$message .= "--PHP-mixed-" . $random_hash . "\r\n";
$message .= "Content-Type: " . $_FILES["photo"]["type"] . "; name=\"" . $_FILES["photo"]["name"] . "\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment\r\n\r\n";
$message .= $attachment;
$message .= "\r\n\r\n--PHP-mixed-" . $random_hash . "\r\n";
}
У меня есть файл JS, который использует Ajax для отправки формы, но я не верю это имеет какое-либо отношение к этой проблеме.
Спасибо за помощь! Я новичок в php.
Рассматривали вы с помощью 3-й партии, как библиотека [SwiftMailer] (http://swiftmailer.org/)? – marekful
@sandeepsure все зависит от того, что означает OP, «используя ajax для отправки». JS может абсолютно отправить форму с вводом файла. Было бы удобно увидеть код JS, чтобы убедиться, что там нет ничего странного. –