I would be grateful if you tell me how to send a photo to an email c <input type="file"> , I only have the name of the photo to come not the image itself, how can I get an image? html:

 <input type="file" id="fileOne"> 

js:

 $("#formModel").submit(function() { $.ajax({ url: "send.php", method: "POST", data: { name: $("#formModel input[name='name']").val(), photo: $("#formModel #fileOne").val() } }); return false; }); 

php:

 <?php $ToEmail = 'myemail.com'; $EmailSubject = 'Анкета'; $mailheader = "From: ".$_POST["email"]."\r\n"; $MESSAGE_BODY .= "Имя: ".$_POST["name"]."\n"; $MESSAGE_BODY .= "Фото 1: ".$_FILES["photo"]."\n"; mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); ?> 

    2 answers 2

    To send a photo via email via html form, you must perform the following steps:

    1. Upload file to server.
    2. Read $_FILES array for uploaded files.
    3. Read the downloaded file and encode its contents in base64
    4. Use mail headers to add a file to an email.
     Content-Type: image/jpg; name=.... Content-Transfer-Encoding: base64 Content-disposition: attachment; file=(тут полученная стрка от base64) 
    • Tell me where do I need to insert it in the code? I just started to deal with Php therefore difficulties at every turn - Dima Vleskov
    • You need to write code, and not insert a piece of my answer to my program. I ask you to ask specific questions, but not freelancing - tasks. - ilyaplot
    • @ilyaplot if you insert the code in the answer, then you should be prepared for the fact that this code will fall into someone else's code. - smellyshovel
    • @DimaVleskov is the headers. They should be in your $ mailHeader. - smellyshovel
    • one
      thanks, it has become clearer what to do - Dima Vleskov

    .val () - contains only the file name, not the file itself. The easiest way to use FormData, but there is a problem with support for older browsers.

    Details with examples but in English here - https://stackoverflow.com/questions/10899384/uploading-both-data-and-files-in-one-form-using-ajax