Why Won't My PHP send mail?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
abrogard
Forum Newbie
Posts: 11
Joined: Sun Apr 04, 2004 6:02 am

Why Won't My PHP send mail?

Post by abrogard »

Can anyone tell me why this script won't send emails?


?php

// Must include botblock.inc
require "botblock.inc";

// Insert any HTML header or PHP code
// echo "<h2>Chime Bot Block</h2>";
// echo "<h3>Demo Form</h3>";

if(validBotBlock())
{
// Codeword is correct.
// Write your own code to process the form.
// For example:

echo "Yup! Codeword was correct :)";
$mailto = 'abrogard@yahoo.com' ;
$subject = "XYFord Feedback Form" ;
$formurl = "feedbackform.php" ;
$homepage = "index.html" ;
$errorurl = "error.html" ;
$thankyouurl = "thankyou.html" ;
$uself = 0;
$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$username = $_POST['username'] ;
$firstname = $_POST['firstname'] ;
$email = $_POST['email'] ;
$comments = $_POST['comments'] ;
$secondname = $_POST['secondname'] ;
$country = $_POST['country'] ;
$phone = $_POST['phone'] ;
// do write to database now---------------------------
$link = mysql_connect("localhost", "xxxxxx", "yyyyyy") or die("Could not connect : " . mysql_error());

$DB = "xyfordco";
$table = "clients";
mysql_select_db($DB) or die ("Database $DB not select.." . mysql_error());

$query = "INSERT INTO $table(username,firstname,secondname,email,country,phone,comments) values('$username','$firstname','$secondname','$email','$country','$phone','$comments')";

if ( ! mysql_query( $query, $link) )
die ( "MySQL error.....<p>" .mysql_error() );

$http_referrer = getenv( "HTTP_REFERER" );
if (!isset($_POST['email'])) {
header( "Location: $formurl" );
exit ;
}


if ( ereg( "[\r\n]", $username ) || ereg( "[\r\n]", $email ) ) {
header( "Location: $errorurl" );
exit ;
}
if (get_magic_quotes_gpc()) {
$comments = stripslashes( $comments );
}

$messageproper =
"This message was sent from:\n" .
"$http_referrer\n" .
"------------------------------------------------------------\n" .
"UserName of sender: $username\n" .
"Firstname of sender: $firstname\n" .
"Secondname of sender : $secondname\n" .
"Country of sender : $country\n" .
"Phone number of sender: $phone\n" .
"Email of sender: $email\n" .
"------------------------- COMMENTS -------------------------\n\n" .
$comments .
"\n\n------------------------------------------------------------\n" ;

mail($mailto, $subject, $messageproper,
"From: \"$username\" <$email>" . $headersep . "Reply-To: \"$username\" <$email>" . $headersep . "X-Mailer: chfeedback.php 2.07" );
header( "Location: $thankyouurl" );
// exit ;
exit ;
}
else
{
$homepage = "index.html";
// Codeword is incorrect.
// Show some error or go back to last page.
// For example:

echo "Bad monkey! Wrong codeword.";
header( "Location: $homepage" );
exit;
}

?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Why Won't My PHP send mail?

Post by social_experiment »

Do you receive any error messages (apart from the error messages your script generates)?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Why Won't My PHP send mail?

Post by John Cartwright »

The mail() function is a demon many of us will not touch with a 100' pole. Thank goodness we have libraries such as http://swiftmailer.org which make emailing a breaze.
Post Reply