-1

I'm not sure why but this has suddenly stopped sending out emails. I have tried to test the email server using mail("email@domain.com","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@domain.com";
$from = "email@domain.com"; 
$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); 
626
  • 1,159
  • 2
  • 16
  • 27
  • 1
    What do you mean by "not working"? Are you getting an error? Is the mail never arriving? Have you checked your smtp server logs? Have you checked your spam folders? – mituw16 Sep 03 '15 at 18:01
  • The mail is never arriving. It's not going into spam. I'll try checking the SMTP logs. – 626 Sep 03 '15 at 18:02
  • Try removing the `@` in front of your mail function if you don't see anything obvious in your smtp server logs. The `@` suppresses errors in PHP. – mituw16 Sep 03 '15 at 18:04
  • There's also no syntax related to files. Such as `$_FILES` http://php.net/manual/en/reserved.variables.files.php. Your form should be a POST method, using a proper enctype. Along with the correct name attribute. All I see is an unassigned `$files` variable. – Funk Forty Niner Sep 03 '15 at 18:10
  • Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Sep 03 '15 at 18:12
  • @ me if you need me, *moving on...* cheers – Funk Forty Niner Sep 03 '15 at 18:15
  • Tried to remove the `@` symbol and added error reporting but I have no errors. I am getting my files from a directory, I have updated my code. – 626 Sep 03 '15 at 18:46
  • You're MIME body is missing a [`close-delimiter`](http://tools.ietf.org/rfcmarkup/2046#page-22). You should add `$message .= "--{$mime_boundary}--\n";` after the attachment loop. – Nisse Engström Sep 05 '15 at 22:26

1 Answers1

1

Looks like my code wasn't the problem. It was an issue with mailing it from info@mydomain.com. Changed $from variable to a gmail email address and it started sending emails again.

626
  • 1,159
  • 2
  • 16
  • 27