0

I made a form with PHP to send mail, When I test it locally it runs fine I receive an email with all the information but when I put it live on a host it says SUCCESS but I never receive the mail. Maybe is something wrong with the code because i have an other form without the ATTACH file and it runs perfecrly

$filenameee =  $_FILES['file']['name'];
$fileName = $_FILES['file']['tmp_name']; 
$name = $_POST['name'];
$email = $_POST['email'];
$title=$_POST['title'];
$prototip=$_POST['prototip'];
$description = $_POST['descripton'];

$message ="Name = ". $name . "\r\n  Email = " . $email . "\r\n \r\n Naslov = ".$title . "\r\n Opis = ".$description; 

$subject ="My email subject";

$mailto = 'levchegochev@gmail.com';  //the email which u want to recv this email




$content = file_get_contents($fileName);
$content = chunk_split(base64_encode($content));

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (RFC)
$eol = "\r\n";

// main header (multipart mandatory)
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;

// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $message . $eol;

// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filenameee . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";

//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
  echo "<script>
                      
  swal({
    title: 'Ви благодариме за вашата апликација!',
    text: 'Kе ве контактираме во рок од 24 часа',
    icon: 'success',
    button: 'Супер!',
  });

           </script>";






 } else {

   echo "<script>alert('Mail was not sent. Please try again later');</script>";

 }
Levche
  • 3
  • 2
  • 1
    Does this answer your question? [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – Definitely not Rafal Oct 20 '21 at 10:10

1 Answers1

0

Updated answer

Set the following parameters in your html form:

<form action="your_target_php_file.php" method="post" enctype="multipart/form-data">

PHP file:

if ($_SERVER['REQUEST_METHOD'] == "POST") {
   // file data from html element input[type='file']
   $file = (object) [
     'tmp_name' => $_FILES['file']['tmp_name'],
     'name' => $_FILES['file']['name'],
     'size' => $_FILES['file']['size'],
     'type' => $_FILES['file']['type'],
     'error' => $_FILES['file']['error'],
   ];

   // other POST data
   $post = (object) [
     'name' => $_POST['name'],
     'email' => $_POST['email'],
     'title' => $_POST['title'],
     'prototip' => $_POST['prototip'],
     'description' =>  $_POST['descripton']
   ];

   // email message
   $message = "Name = ". $post->name . "\r\n  Email = " . $post->email . "\r\n\r\n Naslov = ";
   $message .= $post->title . "\r\n Opis = ".$post->description;

   // email subject
   $subject = "My email subject";

   //the email which u want to recv this email
   $mailto = 'levchegochev@gmail.com';

   //read from the uploaded file
   $handle = fopen($file->tmp_name, "r");
   $content = fread($handle, $file->size);
   fclose($handle);
   $encoded = chunk_split(base64_encode($content));

   // boundary
   $boundary = md5("random");

   // email headers
   $headers[] = "MIME-Version: 1.0";
   $headers[] = "From: " .explode("@", $post->email)[0]. " <{$post->email}>";
   $headers[] = "Reply-To: {$mailto}";
   $headers[] = "Content-Type: multipart/mixed;";
   $headers[] = "boundary = {$boundary}";

   //plain text
   $body = "--{$boundary}\r\n";
   $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
   $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
   $body .= chunk_split(base64_encode($message));

   //attachment
   $body .= "--$boundary\r\n";
   $body .="Content-Type: {$file->type}; name={$file->name}\r\n";
   $body .="Content-Disposition: attachment; filename={$file->name}\r\n";
   $body .="Content-Transfer-Encoding: base64\r\n";
   $body .="X-Attachment-Id: ".rand(1000, 99999)."\r\n\r\n";

   // Attaching the encoded file with email
   $body .= $encoded;
   
   if (mail($post->email, $subject, $body, implode("\r\n", $headers)))
      echo ("<script>
               swal({
                  title: 'Ви благодариме за вашата апликација!',
                  text: 'Kе ве контактираме во рок од 24 часа',
                  icon: 'success',
                  button: 'Супер!',
              });
            </script>");
   } else {
      echo "<script>window.alert('Mail was not sent. Please try again later');</script>";
   }

}

I changed your code a bit, I believe it will work for you.

Petr Fořt Fru-Fru
  • 858
  • 2
  • 8
  • 23