For some reason attachments do not come in letters - the letters themselves come, but there are no attachments.

Here are the markup and scripts

$('#proj_form').submit(function(event) { event.preventDefault(); var formValid = true; $('#proj_form input,textarea').each(function() { if ($(this).attr('id') == 'text-captcha') { return true; } var formGroup = $(this).parents('.form-group'); var glyphicon = formGroup.find('.form-control-feedback'); if (this.checkValidity()) { formGroup.addClass('has-success').removeClass('has-error'); glyphicon.addClass('glyphicon-ok').removeClass('glyphicon-remove'); } else { formGroup.addClass('has-error').removeClass('has-success'); glyphicon.addClass('glyphicon-remove').removeClass('glyphicon-ok'); formValid = false; } }); var name = $("#proj_name").val(); var phone = $("#proj_phone").val(); var time = $("#proj_time").val(); var message = $("#proj_message").val(); var captcha = $("#proj-captcha").val(); var file_data = $('#proj_image').prop('files')[0]; var form_data = new FormData(); form_data.append('file', file_data); form_data.append('name', name); form_data.append('email', email); form_data.append('message', message); form_data.append('captcha', captcha); $.ajax({ type: "POST", url: "feedback/send_proj.php", data: form_data, contentType: false, processData: false, cache: false, success : function(text){ if (text == "success"){ $('#proj_form').hide(); $('#msgSubmit_proj').removeClass('hidden'); } if (text == "invalidcaptcha") { inputCaptcha = $("#text-captcha"); formGroupCaptcha = inputCaptcha.parents('.form-group'); glyphiconCaptcha = formGroupCaptcha.find('.form-control-feedback'); formGroupCaptcha.addClass('has-error').removeClass('has-success'); glyphiconCaptcha.addClass('glyphicon-remove').removeClass('glyphicon-ok'); $('#img-captcha').attr('src', 'feedback/captcha.php?id='+Math.random()+''); $("#text-captcha").val(''); } } }); }); 

The form

 <form id="proj_form" enctype="multipart/form-data"> <div class="form-group col-xs-8 col-xs-offset-2"> <label for="name" class="control-label">Имя*</label> <input type="text" class="form-control" id="proj_name" required/> </div> <div class="form-group col-xs-8 col-xs-offset-2"> <label for="phone" class="control-label">Π’Π΅Π»Π΅Ρ„ΠΎΠ½*</label> <input type="text" class="form-control" id="proj_phone" required/> </div> <div class="form-group col-xs-8 col-xs-offset-2"> <label for="time" class="control-label">ВрСмя Π·Π²ΠΎΠ½ΠΊΠ°</label> <input type="text" class="form-control" id="proj_time"/> </div> <div class="form-group col-xs-8 col-xs-offset-2"> <label for="email" class="control-label">Π‘ΠΎΠΎΠ±Ρ‰Π΅Π½ΠΈΠ΅</label> <textarea id="proj_message" class="form-control" rows="3"></textarea> </div> <div class="form-group col-xs-8 col-xs-offset-2"> <label for="email" class="control-label">ΠŸΡ€ΠΈΠΊΡ€Π΅ΠΏΠΈΡ‚Π΅ Ρ„Π°ΠΉΠ»Ρ‹ ΠΏΡ€ΠΎΠ΅ΠΊΡ‚Π°</label> <input type="file" name="image" id="proj_image" value=""> </div> <div class="form-group col-xs-8 col-xs-offset-2"> <button type="submit" class="btn btn-success send_but">ΠžΡ‚ΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ</button> </div> </form> 

Script handler

 <?php session_start(); $name = $_POST["proj_name"]; $phone = $_POST["proj_phone"]; $time = $_POST["proj_time"]; $message = $_POST["proj_message"]; $emailTo = "email@email.ru"; $subject = "Π’Π΅ΠΌΠ° письма"; $body = "--------------------------------------\n"; $body .= date("Ymd H:i")."\n"; $body .= "Π‘ΠΎΠ΄Π΅Ρ€ΠΆΠΈΠΌΠΎΠ΅ Π·Π°ΠΏΠΎΠ»Π½Π΅Π½Π½Ρ‹Ρ… ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΌ ΠΏΠΎΠ»Π΅ΠΉ:\n"; $body .= "Имя: ".$name."\n"; $body .= "Π’Π΅Π»Π΅Ρ„ΠΎΠ½: ".$phone."\n"; $body .= "ВрСмя Π·Π²ΠΎΠ½ΠΊΠ°: ".$time."\n"; $body .= "Π‘ΠΎΠΎΠ±Ρ‰Π΅Π½ΠΈΠ΅: \n".$message."\n"; $body .= "Π‘ΠΎΠΎΠ±Ρ‰Π΅Π½ΠΈΠ΅: \n".$file."\n"; $sourcePath = $_FILES['file']['tmp_name']; if ( 0 < $_FILES['file']['error'] ) { echo 'Error: ' . $_FILES['file']['error'] . ''; } else { move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/'.$_FILES['file']['name']); } $success = mail($emailTo, $subject, $body, "From: noreply@email.ru \r\n"); if ($success) { echo "success"; } else { echo "invalid"; } 

    2 answers 2

    Where does the attachment cling to the letter? move_uploaded_file () simply saves the file to the server. Download PHPMailer or something similar, or smoke RFC on the attachment attachment.

    • Do not tell me how to rewrite the file to go to the mail? - Batyabest pm

    The issue is resolved. I leave the decision, is it not useful to someone?

     require dirname(__FILE__).'PHPMailerAutoload.php'; $mail = new PHPMailer; $mail->From = 'noreply@site.ru'; $mail->FromName = 'Имя сайта'; $mail->Subject = 'Π’Π΅ΠΌΠ° письма'; $mail->Body = $body; $mail->AddAddress('email@site.ru'); if ( $_FILES['file']['error']==0 ) { move_uploaded_file($_FILES['file']['tmp_name'], 'files/'.$_FILES['file']['name']); $mail->addAttachment('files/'.$_FILES['file']['name']); } $success = $mail->Send();