php mail() script with attachment

<?php
    if ((isset($_POST['contact_phone']) && !empty($_POST['contact_phone'])) || (isset($_POST['contact_email']) && !empty($_POST['contact_email']))){
      $tmpName = $_FILES['attachment']['tmp_name'];
      $fileType = $_FILES['attachment']['type'];
      $fileName = $_FILES['attachment']['name'];

      $headers = "From: noreply@test.com";

      $message = '<h2>Получено новое сообщение. Данные:</h2><br><br><b>Имя: </b>'.$_POST['contact_name'].'<br><b>Телефон: </b>'.$_POST['contact_phone'].'<br><b>Email: </b>'.$_POST['contact_email']."<br><br><hr><p style='color: #ccc;'>Это письмо автоматическое и не требует ответа.</p><br>";

      if (file($tmpName)) {
        /* Reading file ('rb' = read binary)  */
        $file = fopen($tmpName,'rb');
        $data = fread($file,filesize($tmpName));
        fclose($file);

        /* a boundary string */
        $randomVal = md5(time());
        $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";

        /* Header for File Attachment */
        $headers .= "\nMIME-Version: 1.0\n";
        $headers .= "Content-Type: multipart/mixed;\n" ;
        $headers .= " boundary=\"{$mimeBoundary}\"";

        /* Multipart Boundary above message */
        $message = "This is a multi-part message in MIME format.\n\n" .
        "--{$mimeBoundary}\n" .
        "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
        "Content-Transfer-Encoding: 7bit\n\n" .
        $message . "\n\n";

        /* Encoding file data */
        $data = chunk_split(base64_encode($data));

        /* Adding attchment-file to message*/
        $message .= "--{$mimeBoundary}\n" .
        "Content-Type: {$fileType};\n" .
        " name=\"{$fileName}\"\n" .
        "Content-Transfer-Encoding: base64\n\n" .
        $data . "\n\n" .
        "--{$mimeBoundary}--\n";
      }

      mail('email@test.com', 'Новое сообщение из формы сайта', $message, $headers);
    }
  ?>

Поделиться с друзьями

Френдинг

 

Comments

No comments so far.

Leave a Reply

 
(will not be published)
 
 
Comment