Search code examples
phpemailhtml-emailemail-headers

php mail function headers with attachment


I'm not sure why but this has suddenly stopped sending out emails. I have tried to test the email server using mail("[email protected]","test","test message"); and it sends it fine. I have a feeling something is wrong with my headers?

I have tried to send it with and without attachments but it is not working.

Any help would be greatly appreciated.

Thanks in advance!

<?php

extract($_POST);

$dir    = '../uploads/';
$files_temp = scandir($dir, 1);

foreach ($files_temp as $key=>$value) {
  if (strpos($value,$file_id) !== false) {
      $files[] = $value;
  }
}

$message = "";

$message .= "Firm: " . $_POST['firm'] . "\n";
$message .= "Attorney: " . $_POST['attorney'] . "\n";
$message .= "Main Contact: " . $_POST['main_contact'] . "\n";
$message .= "Phone: " . $_POST['phone'] . "\n";
$message .= "Cell: " . $_POST['cell'] . "\n";
$message .= "Fax: " . $_POST['fax'] . "\n";
$message .= "Address: " . $_POST['address'] . "\n";
$message .= "Email: " . $_POST['email'] . "\n";
$message .= "Court County: " . str_replace("_"," ",$_POST['court_county']) . "\n";
$message .= "Court Name: " . $_POST['court_name'] . "\n";
$message .= "Case Type: " . str_replace("_"," ",$_POST['case_type']) . "\n";
$message .= "Appearance Type: " . $_POST['appearance_type'] . "\n";
$message .= "Date: " . $_POST['date'] . "\n";
$message .= "Time: " . $_POST['time'] . "\n";
$message .= "Department: " . $_POST['department'] . "\n";
$message .= "Case Name: " . $_POST['case_name'] . "\n";
$message .= "Case Number: " . $_POST['case_number'] . "\n";
$message .= "Your Client: " . $_POST['your_client'] . "\n";
$message .= "Client Present: " . $client_present_text . "\n";
$message .= "What do you want the attorney to accomplish at this hearing?: " . $_POST['text1'] . "\n";
$message .= "Explain case background: " . $_POST['text2'] . "\n";
$message .= "Signature: " . $_POST['signature'] . "\n";

// email fields: to, from, subject, and so on
$to = "[email protected]";
$from = "[email protected]"; 
$subject ="Appearance form"; 
$headers = "From: $from";


// boundary 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

// headers for attachment 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

// multipart boundary 
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
$message .= "--{$mime_boundary}\n";



// preparing attachments
for($x=0;$x<count($files);$x++){
    $file = fopen($dir.$files[$x],"rb");
    $data = fread($file,filesize($dir.$files[$x]));
    fclose($file);
    $data = chunk_split(base64_encode($data));
    $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
    "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    $message .= "--{$mime_boundary}\n";
}


// send
$ok = @mail($to, $subject, $message, $headers); 

Solution

  • Looks like my code wasn't the problem. It was an issue with mailing it from [email protected]. Changed $from variable to a gmail email address and it started sending emails again.