-1

My form did send all info except of file/ How it is possible to fix it?

This is input file:

<input type="file" name="file" placeholder="ЗАГРУЗИТЬ ЧЕК" id="file_kd" required>
<br></p>

This is code php mail:

<?php
header('Refresh: 0; URL=http://yougotit.agency/kodabra/thank-you.php'); // 
переадресация на страницу спасибо
$to = "stanislav.mandrik@gmail.com"; // емайл получателя данных из формы 
$tema = "Kodabra - заявка успешно отправлена!"; // тема полученного емайла 
$from = "Kodabra <no-reply@kodabra.com>";
$photo = $_FILES['file']['name'];
$message = "Ваше имя: ".$_POST['kdname']."<br>";
$message .= "E-mail: ".$_POST['kdemail']."<br>"; 
$message .= "Номер телефона: ".$_POST['kdphone']."<br>";
$message .= "Артикул модели (указан на упаковке): ".$_POST['kdartic']."<br>"; 
$message .= "Номер чека: ".$_POST['kdbill']."<br>";  
$message .= "Комментарий: ".$_POST['kdcomment']."<br>"; 
$message .= "Согласился на обработку персональных данных. ".$_POST['kdagree']."<br>"; 
$message .= "Фото чека: ".($photo)."\n";
$headers = "MIME-Version: 1.0"."\r\n".
           "Content-type: text/html; charset=\"utf-8\""."\r\n".
           "From: $from"."\r\n";

mail($to, $tema, $message, $headers);
?>
georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • no, this is example of how to attach own file from the directory, but i need that people upload their own files to form and send it to email. – Stanislav Mandrik Aug 11 '18 at 08:24
  • It is required to upload the file to your server then send it using mail(). [see this to upload file to server](https://www.w3schools.com/php/php_file_upload.asp) – M0ns1f Aug 11 '18 at 08:28
  • Yea, thats work good, and uploaded to my folder, and the point of my post is HOW to attach this file uploaded to email as an attachment? – Stanislav Mandrik Aug 11 '18 at 08:33
  • You *can* attach files with `mail()` but it's too easy to screw the format. It's not really worth the effort. I suggest you use a third-party library like PHPMailer or Swift Mailer. But however you attach the file, you really need to learn first how [PHP file uploads](http://php.net/manual/en/features.file-upload.php) work: you're just using the file name and discarding its contents. – Álvaro González Aug 11 '18 at 17:07
  • Use PHPMailer... It will save you a bunch of headaches – Ikhlak S. Aug 11 '18 at 19:08

2 Answers2

0

from the answer that i marked duplicate please don't upvote this

To send an email with attachment we need to use the multipart/mixed MIME type that specifies that mixed types will be included in the email. Moreover, we want to use multipart/alternative MIME type to send both plain-text and HTML version of the email.Have a look at the example:

    <?php 
    //define the receiver of the email 
    $to = 'youraddress@example.com'; 
    //define the subject of the email 
    $subject = 'Test email with attachment'; 
    //create a boundary string. It must be unique 
    //so we use the MD5 algorithm to generate a random hash 
    $random_hash = md5(date('r', time())); 
    //define the headers we want passed. Note that they are separated with \r\n 
    $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com"; 
    //add boundary string and mime type specification 
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
    //read the atachment file contents into a string,
    //encode it with MIME base64,
    //and split it into smaller chunks
    $attachment = chunk_split(base64_encode(file_get_contents('attachment.zip'))); 
    //define the body of the message. 
    ob_start(); //Turn on output buffering 
    ?> 
    --PHP-mixed-<?php echo $random_hash; ?>  
    Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

    --PHP-alt-<?php echo $random_hash; ?>  
    Content-Type: text/plain; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit

    Hello World!!! 
    This is simple text email message. 

    --PHP-alt-<?php echo $random_hash; ?>  
    Content-Type: text/html; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit

    <h2>Hello World!</h2> 
    <p>This is something with <b>HTML</b> formatting.</p> 

    --PHP-alt-<?php echo $random_hash; ?>-- 

    --PHP-mixed-<?php echo $random_hash; ?>  
    Content-Type: application/zip; name="attachment.zip"  
    Content-Transfer-Encoding: base64  
    Content-Disposition: attachment  

    <?php echo $attachment; ?> 
    --PHP-mixed-<?php echo $random_hash; ?>-- 

    <?php 
    //copy current buffer contents into $message variable and delete current output buffer 
    $message = ob_get_clean(); 
    //send the email 
    $mail_sent = @mail( $to, $subject, $message, $headers ); 
    //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
    echo $mail_sent ? "Mail sent" : "Mail failed"; 
    ?>

As you can see, sending an email with attachment is easy to accomplish. In the preceding example we have multipart/mixed MIME type, and inside it we have multipart/alternative MIME type that specifies two versions of the email. To include an attachment to our message, we read the data from the specified file into a string, encode it with base64, split it in smaller chunks to make sure that it matches the MIME specifications and then include it as an attachment.

Taken from here.

M0ns1f
  • 2,705
  • 3
  • 15
  • 25
  • Again this is different compare to what i asking about...: 1. I have a form with input file 2. I have code above to send it to email. 3. I need that every person who attach the file own and always different will be sent it to my email. – Stanislav Mandrik Aug 11 '18 at 08:47
0

In order to send email with file attachments you have to split the message into multiple parts with different encoding - here's how I do it. Note that there is a section for plain text information, and then it changes content encoding and adds any attachments by reading in the byte stream and re-encoding to base64 format and adding that to the message body, specifing content type, its file name, etc.

Of course, this all assumes that you have a working mail set up and a mail() of a plain text message works fine...

<?php
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "From: $from_name <$from_address>\r\n";
$headers .= "Date: " . date("Ymd H:i:s") . "\r\n";
$headers .= "Reply-To: $from_name <$from_address>\r\n";            
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n";
$headers .= "X-Mailer: ".$_SERVER['PHP_SELF']. "?id=". $_SERVER['UNIQUE_ID']. "\r\n";

$separator=md5(time());
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n";
$headers .= "This is a MIME encoded message.\r\n";
// text message as normal for f2m
$messageBody="--".$separator."\r\n";
$messageBody.="Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$messageBody.="Content-Transfer-Encoding: 8bit\r\n";
$messageBody.="\r\n".$message_body."\r\n\r\n";
// add attachments
// $attachments is basically the $_FILES array
for($i=0;$i<count($attachments);$i++){
    $attachcontent=chunk_split(base64_encode(file_get_contents($attachments[$i]['tmp_name'])));
    $messageBody.="--".$separator."\r\n";
    $messageBody.="Content-Type: application/octet-stream; name=\"".$attachments[$i]['name']."\"\r\n";
    $messageBody.="Content-Transfer-Encoding: base64\r\n";
    $messageBody.="Content-Disposition: attachment; filename=\"".$attachments[$i]['name']."\"\r\n";
    $messageBody.="\r\n".$attachcontent."\r\n";
}
$messageBody.="--" . $separator . "--";
mail($to_address, $subject, $messageBody, $headers);
?>
ivanivan
  • 2,155
  • 2
  • 10
  • 11