Jump to content

PHP Mail - clients not receiving all of the emails


joharm

Recommended Posts

Hi I'm a bit of a PHP newbie, so forgive me if I'm not explaining myself very well.

 

I set up a simple PHP booking form for a client (Windows hosting with Supanames running PHP4), which works fine. However, the client is telling me they are not receiving every one of the booking emails.

 

I have checked all of the simple things, such as checking their spam folder etc. To me it now seems that it could be to do with the way PHP is set up on the server.

 

phpinfo() tells me:

 

PHP Version 4.4.8
System:  Windows NT SUPANETW-OVO0LL 5.2 build 3790
sendmail_from: [email protected]
sendmail_path: no value no value 
SMTP: localhost 

 

Is this the right setup for Windows hosting? If the mail gets sent almost every time, then surely it's nothing to do with my code? Would the client receive a Failed Mail Delivery if the mail didn't work?

 

Any ideas appreciated.

 

 

 

Here is my code:

 

<?
$course=$_POST['course'];
$date=$_POST['date'];
$location=$_POST['location'];
$cost=$_POST['cost'];
$title=$_POST['title'];
$email=$_POST['email'];
$surname=$_POST['surname'];
$firstname=$_POST['firstname'];
$jobtitle=$_POST['jobtitle'];
$company=$_POST['company'];
$address=$_POST['address'];
$comments=$_POST['comments'];
$postcode=$_POST['postcode'];
$tel=$_POST['tel'];
$mobile=$_POST['mobile'];
$fax=$_POST['fax'];
$titledel1=$_POST['titledel1'];
$titledel2=$_POST['titledel2'];
$titledel3=$_POST['titledel3'];
$titledel4=$_POST['titledel4'];
$firstnamedel1=$_POST['firstnamedel1'];
$firstnamedel2=$_POST['firstnamedel2'];
$firstnamedel3=$_POST['firstnamedel3'];
$firstnamedel4=$_POST['firstnamedel4'];
$surnamedel1=$_POST['lastnamedel1'];
$surnamedel2=$_POST['lastnamedel2'];
$surnamedel3=$_POST['lastnamedel3'];
$surnamedel4=$_POST['lastnamedel4'];
$jobtitledel1=$_POST['jobtitledel1'];
$jobtitledel2=$_POST['jobtitledel2'];
$jobtitledel3=$_POST['jobtitledel3'];
$jobtitledel4=$_POST['jobtitledel4'];
$checkbox=$_POST['checkbox'];
$po=$_POST['po'];

function valid_email($email) { 
  // First, we check that there's one @ symbol, and that the lengths are right 
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) { 
    // Email invalid because wrong number of characters in one section, or wrong number of @ symbols. 
    return false; 
  } 
  // Split it into sections to make life easier 
  $email_array = explode("@", $email); 
  $local_array = explode(".", $email_array[0]); 
  for ($i = 0; $i < sizeof($local_array); $i++) { 
     if (!ereg("^(([A-Za-z0-9!#$%&#038;'*+/=?^_`{|}~-][A-Za-z0-9!#$%&#038;'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) { 
      return false; 
    } 
  }   
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name 
    $domain_array = explode(".", $email_array[1]); 
    if (sizeof($domain_array) < 2) { 
        return false; // Not enough parts to domain 
    } 
    for ($i = 0; $i < sizeof($domain_array); $i++) { 
      if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) { 
        return false; 
      } 
    } 
  } 
  return true; 
} 



if (($course == "") or ($location == "") or ($title == "") or ($email == "") or ($surname == "") or ($date == "") or ($firstname == "") or ($company == "") or ($address == "") or ($postcode == "") or ($tel == "") or ($checkbox == "") or (valid_email($email) == false)) {
header("Location:booking_noconfirm.php?date=$date&location=$location&course=$course&cost=$cost&title=$title&firstname=$firstname&surname=$surname&email=$email&jobtitle=$jobtitle&company=$company&address=$address&comments=$comments&postcode=$postcode&tel=$tel&mobile=$mobile&fax=$fax&titledel1=$titledel1&titledel2=$titledel2&titledel3=$titledel3&titledel4=$titledel4&firstnamedel1=$firstnamedel1&firstnamedel2=$firstnamedel2&firstnamedel3=$firstnamedel3&firstnamedel4=$firstnamedel4&lastnamedel1=$lastnamedel1&lastnamedel2=$lastnamedel2&lastnamedel3=$lastnamedel3&lastnamedel4=$lastnamedel4&jobtitledel1=$jobtitledel1&jobtitledel2=$jobtitledel2&jobtitledel3=$jobtitledel3&jobtitledel4=$jobtitledel4&po=$po");
}
else { 

$to  = '[email protected]';
$subject = 'New booking';
$message ="<b>Booking from the Benchmark Business Training website</b><br><br>Course title: <b>$course</b><br>Course date: <b>$date</b><br>Location: <b>$location</b><br>Cost: <b>£ $cost</b><br><br><u>Booker Information:</u><br><br>Title: <b>$title</b><br>First name: <b>$firstname</b><br>Last name: <b>$surname</b><br>Job title: <b>$jobtitle</b><br>Company name: <b>$company</b><br>Address: <b>$address</b><br>Postcode: <b>$postcode</b><br>Daytime Tel: <b>$tel</b><br>Mobile: <b>$mobile</b><br>Fax: <b>$fax</b><br>Email: <b>$email</b><br>Comments: <b>$comments</b><br><br><u>Delegates Information:</u><br><br>$titledel1 $firstnamedel1 $surnamedel1 - $jobtitledel1<br>$titledel2 $firstnamedel2 $surnamedel2 - $jobtitledel2<br>$titledel3 $firstnamedel3 $surnamedel3 - $jobtitledel3<br>$titledel4 $firstnamedel4 $surnamedel4 - $jobtitledel4<br><br>Purchase Order Number: <b>$po</b>";

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: [email protected] <[email protected]>' . "\r\n";
$headers .= 'From: Benchmark website booking form' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

}

?>

You need a valid email address in the "From:" header, you don't. You just have a string. Also, you don't need to put the "To:" line into the header, since that is the first parameter to the mail() function:

<?php
$headers .= "From: Benchmark website booking form <[email protected]\r\n";
?>

 

Ken

How do they know some emails are not being sent/received?

 

Is the email address [email protected] hosted on the sending mail server or is this hosted at a different server?

 

Start by checking the web server error log file. There may be php generated errors concerning the mail() function call (assuming that error_reporting is set to E_ALL, which if it is not, should be changed to E_ALL.)

 

Some apparent problems -

 

The From: field in the header, when present, is expected to be a properly formatted email address and it is expected to be an email address hosted at the sending mail server.

 

By putting the email address into both the first parameter of the mail() function and as a To: field in the header, it should be causing two emails to be sent. This could trigger some email filters to regard the emails as spam.

 

You need to test the results of calling the mail() function and if a FALSE is returned (either the sending mail server was not available or it did not accept the mail for sending) output a meaningful message to the user and log the fact to a file using the php error_log() function.

 

You should also be using the php error_log() function to log both successful form submission (the mail() function is called) and failed activity (the header() redirect was called) so that you know what is happening on the server.

Many thanks, those are all things to look into.

 

The client knows they are not getting emails, as people have come to them claiming they have already booked. I don't think it happens very often, but enough for them to mention the problem.

 

The email address is hosted on the same server, and I have corrected the From: header and taken out the To: header so this might make a difference.

 

There isn't a web error log on this hosting.

 

How would I go about testing the results of mail()? My normal process is to just see whether the email comes through! Do I need an if statement?

 

Thanks for all your help.

The mail() function call returns a TRUE or FALSE as follows -

 

Return Values

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

 

This can be used in a simple if/else statement (along with your own error logging) -

 

<?php
ini_set("track_errors","1"); // cause last php error generated to be put into $php_errormsg

... your code here ...

if(mail(... your parameters here ...))
{
// function call returned TRUE - the mail server was available and accepted the email for sending
echo "User message here... Thank you for submitting your information...";
error_log("Mail accepted by sending mail server... put other details here [datetime, who, ip address...]\r\n", 3, "mail.log");
} else {
// function call returned FALSE - no mail server was available (bad configuration, it is down...) or the email was not accepted for sending
echo "User message here ... Your information could not be processed...";
error_log("You messed up! $php_errormsg... put other details here [datetime, who, ip address...]\r\n", 3, "errormail.log");
}
?>

Email servers are up and down all the time. Automated email filtering/blocking software can temporarily cause a mail server to discard your emails if a spam bot script just hit your form processing code with a ton of spam. Your script is only validating the email address. All the fields need to be validated to insure they contain expected content.

 

If your code has absolutely no error checking, error reporting, error logging, and error recovery logic to get it to tell you what it is doing, you will never know if it is working or what is happening when it is failing.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.