Answer the question
In order to leave comments, you need to log in
How to send an attachment along with an email?
Good day .. for a couple of days now I can’t send a file to the mail .. I don’t know what the problem is ..
Instead of an attachment, a noname file comes (
PS No need to just write, use phpmailer, etc.
Thank you very much )))
<form action="" class="s2-form">
<h2 class="form-heading">Оставьте предварительную заявку и мы свяжемся с вами в течении 5 минут.</h2>
<input type="text" class="s2-input input" name="name" placeholder="Имя..."/>
<input type="text" class="s2-input input" name="phone" placeholder="Телефон..."/>
<label for="file" class="file-btn">Прикрепить фото</label>
<input type="file" class="file" id="file" name="file">
<button class="btn s2-btn btn_red">Заказать звонок</button>
</form>
<?php
$test = '[email protected]';
$to_email = "[email protected]";
$from_email = "test";
$subject = "Входящая заявка с сайта";
$name = $_POST['name'];
$phone = $_POST['phone'];
$file = $_FILES['file'];
$message ="Имя: ".$name."\n".
"Номер телефона: ".$phone."\n";
$fileName = $file['name'];
$attachment = chunk_split(base64_encode(file_get_contents($file['tmp_name'])));
$message .= "Прикрепленный файл: ".$fileName."\n";
$boundary =md5(date('r', time()));
$headers = "From: [email protected]\r\nReply-To: [email protected]";
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
$message="This is a multi-part message in MIME format.
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
--_2_$boundary
Content-Type: text/plain; charset=\"utf-8\"
Content-Transfer-Encoding: 7bit
$message
--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$fileName\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--";
mail($to_email, $subject, $message, $headers);
?>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question