Forum Moderators: coopster
For some reason this PHP script isn't sending mail. I'm not sure what is going wrong. I know the array is being posted properly so the data is there.
Can someone please offer any advice on troubleshooting this?
It prints the "mail sent" message but nothing comes into the inbox.
The host is godaddy.
Is this script doing something unusual or sending mail in an abnormal way that maybe my server needs to be configured for it?
<?php
/*******************************************
/*services_process.php
/*Author: #*$!X (email@example.com)
/*
/*Desc:This page process and checks the submitted form.
/*
/*variablemeaning
/*---------------
/*$FieldCheckInstance of the FieldCheck class.
/*
/******************************************/session_start();
//include Field Check Class
include("classes/FieldCheck.php");
$FieldCheck = new FieldCheck;
//print_r($_POST);
extract($_POST);
/*Beginning*/
if (!$FieldCheck->checkName($name)){ $errors['name'] = "Name is not valid.";}
if (!$FieldCheck->checkStreetAddress($address)){ $errors['address'] = "Address is not valid.";}
if (!$FieldCheck->isEmpty($state)){ $errors['state'] = "State is not valid.";}
if (!$FieldCheck->isEmpty($city)){ $errors['city'] = "City is not valid.";}
if (!$FieldCheck->checkZipCode($zip)){ $errors['zip'] = "Zip is not valid.";}
/*Second Page*/
if (!$FieldCheck->checkPhoneNumber($daytime_phone)){ $errors['daytime_phone'] = "Phone Number is not valid.";}
if (!$FieldCheck->checkPhoneNumber($nighttime_phone)){ $errors['nighttime_phone'] = "Nighttime Phone Number is not valid.";}
//if (!$FieldCheck->checkPhoneNumber($fax)){ $errors['fax'] = "Fax Number is not valid.";}
if (!$FieldCheck->checkEmail($email)){ $errors['email'] = "Email is not valid.";}
if (!$FieldCheck->isEmpty($my_question)){ $errors['my_question'] = "Entry is not valid.";}
foreach($_POST as $key=>$value){
$fields[$key] = $value;
}
//print_r($_POST);
//check for error messages
if (isset($errors)) {
$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;
$_SESSION['message'] = "Errors were found in some fields. Please review your entries.";
$url = "./service_request.php?action=errors";
header ("Location: $url");
exit;
}else{
//define the receiver of the email
$to = 'email@example.com';
//define the subject of the email
$subject = 'Services Request Form';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: Website";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head profile="http://www.w3.org/2000/08/w3c-synd/#"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Example Website" />
<link rel="stylesheet" type="text/css" href="http://example.com/clients/fourseasons/css/reset.css" />
<link rel="stylesheet" type="text/css" href="http://example.com/clients/fourseasons/css/style_front.css" />
<style type="text/css">
body {text-align:center;font-family:arial;font-size:14px;padding:20px;}
table {width:700px;}
table tr{}
table tr.odd{background-color:#F0F0F0;}
table tr th{padding:10px;background-color:black;color:white;font-weight:bold;}
table tr td{padding:10px;}
table tr td.label{text-align:right;width:300px;}
table tr td.data{text-align:left;width:400px;}
</style>
</head>
<body>
<table border="1">
<tr>
<th colspan="2">Services Request</th>
</tr>
<tr class="odd">
<td class="label">System Name:</td>
<td class="data"><?php echo $name_of_your_system;?></td>
</tr>
<tr>
<td class="label">Name:</td>
<td class="data"><?php echo $name;?></td>
</tr>
<tr class="odd">
<td class="label">Address:</td>
<td class="data"><?php echo $address;?></td>
</tr>
<tr>
<td class="label">State:</td>
<td class="data"><?php echo $state;?></td>
</tr>
<tr class="odd">
<td class="label">City:</td>
<td class="data"><?php echo $city;?></td>
</tr>
<tr>
<td class="label">Zip:</td>
<td class="data"><?php echo $zip;?></td>
</tr>
<tr class="odd">
<td class="label">Daytime Phone:</td>
<td class="data"><?php echo $daytime_phone;?></td>
</tr>
<tr>
<td class="label">Nighttime Phone:</td>
<td class="data"><?php echo $nighttime_phone;?></td>
</tr>
<tr class="odd">
<td class="label">Fax Number:</td>
<td class="data"><?php echo $fax;?></td>
</tr>
<tr>
<td class="label">Email Address:</td>
<td class="data"><?php echo $email;?></td>
</tr>
<tr class="odd">
<td class="label">Interested In:</td>
<td class="data"><?php echo $indoor_air_quality_products;?></td>
</tr>
<tr>
<td class="label"><?php echo $new_residential_construction;?></td>
<td class="data"><?php echo $service_or_installation;?></td>
</tr>
<tr class="odd">
<td class="label"><?php echo $maintenance;?></td>
<td class="data"><?php echo $high_efficiency_filters;?></td>
</tr>
<tr>
<td class="label"><?php echo $energy_consultation;?></td>
<td class="data"><?php echo $duct_cleaning;?></td>
</tr>
<tr class="odd">
<td class="label"><?php echo $energy_savings_plan;?></td>
<td class="data"><?php echo $other;?></td>
</tr>
<tr>
<td class="label">This is my problem:</td>
<td class="data"><?php echo $my_question;?></td>
</tr>
<tr class="odd">
<td class="label">Homeowner?</td>
<td class="data"><?php echo $homeowner;?></td>
</tr>
<tr>
<td class="label">Do you own an Energy Savings Plan?:</td>
<td class="data"><?php echo $do_you_own_an_energy_savings_plan;?></td>
</tr>
<tr class="odd">
<td class="label">Would you like to sign up for one now?</td>
<td class="data"><?php echo $sign_up_for_one;?></td>
</tr>
<tr>
<td class="label">Would you like a salesman to contact you?:</td>
<td class="data"><?php echo $salesman_contact;?></td>
</tr>
</table>
</body>
</html>
--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
}
?>
[edited by: dreamcatcher at 6:44 pm (utc) on Jan. 5, 2010]
[edit reason] Removed specifics. [/edit]
I know the array is being posted properly so the data is there.
Are you sure? This is where I would start...
Have you actually printed the array() to see the information and array pieces being passed? I ask because this was the exact issue I found when a form which had worked for years quit working suddenly and I was asked to fix it. For some reason, in some PHP sub-version, arrays can be posted correctly according to the manual but the information is not where it should be. I finally had to switch the form names and use a for() to loop through the variables and get the info out.
I haven't read the entire post, this is just the first thing that comes to mind, so it could be something else entirely...
Londrum: That may be it. I'll contact my host. Sometimes it comes through fine, sometimes it doesn't. That may have to do with it. Can you post an example of the header string you're referring to so I can use it as an example?