I've been messing with this form nearly 2 days now and can't get it to deliver the email when sent. It seemed to be alright until I tried changing where it would be sent based on the location.

As of now, it redirects to the index page (indicating it was sent) yet I receive zero submissions ...

Anyone know what's going on here or what i may be doing incorrectly?

Thank you ~

$locations_to_email = array('Austin'=>'myemail',
                             'San Antonio'=>'myemail',
                             'New Jersey'=>'myemail'
$Subject = "Request Service Form";
$Locations = htmlspecialchars(trim(stripslashes($_POST['Locations'])));
$Contact = htmlspecialchars(trim(stripslashes($_POST['Contact'])));
$Company = htmlspecialchars(trim(stripslashes($_POST['Company'])));
$ContactPhone = htmlspecialchars(trim(stripslashes($_POST['ContactPhone']))); 
$ContactMobile = htmlspecialchars(trim(stripslashes($_POST['ContactMobile'])));
$SitePhone = htmlspecialchars(trim(stripslashes($_POST['SitePhone'])));
$Email = htmlspecialchars(trim(stripslashes($_POST['Email'])));
$RequestedDate = htmlspecialchars(trim(stripslashes($_POST['RequestedDate'])));
$EmergencyResponse = htmlspecialchars(trim(stripslashes($_POST['EmergencyResponse']))); 
$WorkDoneRemotely = htmlspecialchars(trim(stripslashes($_POST['WorkDoneRemotely'])));
$ExtensionsInvolved = htmlspecialchars(trim(stripslashes($_POST['ExtensionsInvolved'])));
$WorkPerformed = htmlspecialchars(trim(stripslashes($_POST['WorkPerformed'])));
$Description = htmlspecialchars(trim(stripslashes($_POST['Description'])));
$EmailFrom = "ICS Request Service Form";
$EmailTo = isset($locations_to_email[$Locations])?$locations_to_email[$Locations]:'myemail';

// validation
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
// prepare email body text
$Body = "";
$Body .= "Contact: ";
$Body .= $Contact;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $Company;
$Body .= "\n";
$Body .= "ContactPhone: ";
$Body .= $ContactPhone;
$Body .= "\n";
$Body .= "ContactMobile: ";
$Body .= $ContactMobile;
$Body .= "\n";
$Body .= "SitePhone: ";
$Body .= $SitePhone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "EmergencyResponse: ";
$Body .= $EmergencyResponse;
$Body .= "\n";
$Body .= "WorkDoneRemotely: ";
$Body .= $WorkDoneRemotely;
$Body .= "\n";
$Body .= "ExtensionsInvolved: ";
$Body .= $ExtensionsInvolved;
$Body .= "\n";
$Body .= "WorkPerformed: ";
$Body .= $WorkPerformed;
$Body .= "\n";
$Body .= "Location: ";
$Body .= $Location;
$Body .= "\n";
$Body .= "Description: ";
$Body .= $Description;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";

2 answers

Doug 1095

I don't have a solution for you, but some general debugging tips. The problem may be with the mail handler on your server and PHP may not know about it. As the manual says, "It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination."

So first try creating a simplified PHP file to test your server's mail sending abilities:

$EmailTo = 'myemail@example.com';
$Subject = 'Test';
$Body = 'test';
$success = mail($EmailTo, $Subject, $Body);

Pull up this file in your browser. If you get a success message (true), but don't receive the email, then you know the problem is not with the script.

One more tip: the PHP header function is a much more efficient way of redirecting the browser than printing meta headers:

header('Location: http://www.example.com/error.htm');

The meta refresh causes an additional round-trip to the browser and a noticeable delay for the user.

Answered over 8 years ago by Doug
  • +1 to what Doug said about both issues. DondeEstaMiCulo over 8 years ago

hard to be sure but I suspect that your form is setting $Location, but you're trying to choose where based on a variable $Locations (plural) if $Locations isn't set then the content will be sent to the last option in this line:

$EmailTo = isset($locations_to_email[$Locations])?$locations_to_email[$Locations]:'myemail';

so, (a) is the form field for locations, Location or Locations ? and (b) is the default for no location found a valid email address? (c) should this:

$Locations = htmlspecialchars(trim(stripslashes($_POST['Locations'])));


$Locations = htmlspecialchars(trim(stripslashes($_POST['Location'])));

(no 's')

I also question the sense of using isset to choose a email address, as in your case you're setting the location ($Locations=), but it might be blank - if the variable doesn't match what's posted.

Answered over 8 years ago by Tony Crockford
  • I believe she's got it right with `isset`. A "location" name gets posted and she's comparing it to her list of $locations_to_email. If that location exists in her array, then an email is sent to that corresponding email address. If not, then it will default to 'myemail'. The code looks sound. My guess it's with the email server. DondeEstaMiCulo over 8 years ago
  • disagree, the $locations is set to the value of $_POST['Locations'] but the form is sending a value for location - inferred from the line that says: $Body .= "Location: "; $Body .= $Location; to properly fix we need the HTML form code or a print_r of the POST Tony Crockford over 8 years ago