Prompt a working example with sending a file (attachment) from the form to the mail. There are many examples on the Internet, but it all comes down to either the large PHPMailer library or non-working examples. What is this secret magic?

    1 answer 1

    Here is my working version:

    HTML

    <form method="POST" enctype="multipart/form-data"> <input type="text" name="name" placeholder="Ваше имя" /> <input type="text" name="sender" placeholder="Куда мне ответить ( ваша почта )" /> <input type="text" name="subject" placeholder="Тема сообщения" /> <textarea name="message"></textarea> <input type="hidden" name="emailto" value="example@mail.ru" /> <input type="file" name="toupload" /> <input type="submit" name="submit" value="Отправить" /> </form> 

    Php

     <?php function mailX($emailfrom, $emailto, $subject, $message = "Текст сообщения которое будет отправлено",$filename) { $un = strtoupper(uniqid(time())); $headers = "From: $emailfrom <noreply@site.mail>\n"; $headers .= "To: $emailto\n"; $headers .= "Subject: $subject\n"; $headers .= "X-Mailer: PHPMail Tool\n"; $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-Type:multipart/mixed;"; $headers .= "boundary=\"----------".$un."\"\n\n"; $message .= "------------".$un."\nContent-Type:text/html;\n"; $message .= "Content-Transfer-Encoding: 8bit\n\n$text\n\n"; $f = fopen($filename,"rb"); $message .= "------------".$un."\n"; $message .= "Content-Type: application/octetstream;"; $message .= "name=\"".basename($filename)."\"\n"; $message .= "Content-Transfer-Encoding:base64\n"; $message .= "Content-Disposition:attachment;"; $message .= "filename=\"".basename($filename)."\"\n\n"; $message .= chunk_split(base64_encode(fread($f,filesize($filename))))."\n"; mail($emailto, $subject, $message, $headers, $emailfrom); } if ( ISSET($_POST['submit']) ) { //Сделаем более-менее прилежным вид письма $message = 'Привет. На твой ящик пришло письмо от '.$_POST['name'].' '.($_POST['sender']).' !<br/> С таким содержанием '.$_POST['message']; mailX('Форма обратной связи', $_POST['emailto'], $_POST['subject'],$message, $_FILES['toupload']['name']); } ?> 
    • Maybe I'm doing something wrong again? Here I replaced the mail with my own (<input type = "hidden" name = "emailto" value = "..." />), placed the form in the index.html PHP code in the mail.php file. All this is uploaded to the host and nothing works and gives an error after pressing the "send" button. - Alexander
    • @ Alexander, what mistakes gives? Can I go to the studio? - sashatexb
    • And action = "* .php" need to add the same? Then it produces errors like this: Warning: fopen (привет.txt): failed to open stream: No such file or directory in /home/u842147903/public_html/mail.php on line 14 ‚.Txt in /home/u842147903/public_html/mail.php on line 21 Warning: fread () expects parameter 1 to be resource, boolean given in /home/u842147903/public_html/mail.php on line 21 If without action =" * .php "there are no errors. But in that and in that case it does not work for some reason, I tried on different mails. As a result, one time came, no more - Alexander
    • @ Alexander if you take out the php file separately, then action = "mail.php" is necessary. - sashatexb