Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on

php email problem

See code.

Why does this email fail?

Note: This is the ENTIRE php file.
<?
// set up database
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	$headers .= "From: info@villageairportvan.com\r\n";	
	$headers .= 'Bcc: richard@rkassociates.com' . "\r\n";	
	$subj = "Online Contact Form Submission";	
	$tostr = "jeremiahedelgado@gmail.com";
	$msg = "Web site Contact Form submitted as follows:<br><br>";
	$msg = $msg . "Contact Name: " . $_POST['your-name'] . " " . $_POST['LastName'] . "<br><br>";
	$msg = $msg . "Contact Email Address: " . $_POST['your-email'] . "<br><br>";
	$msg = $msg . "Subject: " . $_POST['your-subject'] . "<br><br>";
	$msg = $msg . "Message: " . $_POST['your-message'] . "<br><br>";
	$m = mail($tostr, $subj, $msg, $headers);
	echo "mail result = " .$m . "<br>"
	//header("Location: index.php")
?>

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

What do you mean by "fail?"
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Richard Korts

ASKER

To Ray_Paseur:

I modified the script. See code. It returns false.

I also displayed the $msg variable.

You can try it at http://s385518869.onlinehome.us/contact.php
<?
// set up database
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	$headers .= "From: info@villageairportvan.com\r\n";	
	$headers .= 'Bcc: richard@rkassociates.com' . "\r\n";	
	$subj = "Online Contact Form Submission";	
	$tostr = "jeremiahedelgado@gmail.com";
	$msg = "Web site Contact Form submitted as follows:<br><br>";
	$msg = $msg . "Contact Name: " . $_POST['your-name'] . " " . $_POST['LastName'] . "<br><br>";
	$msg = $msg . "Contact Email Address: " . $_POST['your-email'] . "<br><br>";
	$msg = $msg . "Subject: " . $_POST['your-subject'] . "<br><br>";
	$msg = $msg . "Message: " . $_POST['your-message'] . "<br><br>";
	echo "msg = " . $msg . "<br>";
	$m = mail($tostr, $subj, $msg, $headers);
	if ($m) {
		echo "mail returned true" . "<br>";
	} else {
		echo "Mail returned false<br>";
	}		
	//header("Location: index.php")
?>

Open in new window

Do you have error messages turned on in your php.ini?  you probably have a misconfigured email setup and that is causing the email to fail.  If error reporting is off you won't see anything.

Put this at the top of your script and see what happens:

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

smadeira:

I put that there & did it again.

Nothing
 
You can try it at http://s385518869.onlinehome.us/contact.php
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sudaraka:

That worked, I got the email.
Ok, so it seems the server configuration is fine.

Did you use the same To email address in both cases?
There could be a address/host relay issue, can you check the mail log?
This worked perfectly on my server.  I received the email.  Of course the $POST information was not there, but that is beside the point.
<?php // RAY_temp_rkorts.php
error_reporting(E_ALL);
echo "<pre>";

// set up database
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	$headers .= "From: info@villageairportvan.com\r\n";
	$headers .= 'Bcc: richard@rkassociates.com' . "\r\n";
	$subj = "Online Contact Form Submission";
	$tostr = "ray.paseur@gmail.com";
	$msg = "Web site Contact Form submitted as follows:<br><br>";
	$msg = $msg . "Contact Name: " . $_POST['your-name'] . " " . $_POST['LastName'] . "<br><br>";
	$msg = $msg . "Contact Email Address: " . $_POST['your-email'] . "<br><br>";
	$msg = $msg . "Subject: " . $_POST['your-subject'] . "<br><br>";
	$msg = $msg . "Message: " . $_POST['your-message'] . "<br><br>";
	echo "msg = " . $msg . "<br>";
	$m = mail($tostr, $subj, $msg, $headers);
	if ($m) {
		echo "mail returned true" . "<br>";
	} else {
		echo "Mail returned false<br>";
	}
	//header("Location: index.php")

Open in new window

The design pattern for a form-to-email script is pretty well understood.  This is my teaching example.  See if it makes sense to you.
<?php // RAY_form_to_email.php
error_reporting(E_ALL);



// SEND MAIL FROM A FORM



// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "NoReply@Your.org";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS - CHANGE THESE FOR YOUR WORK
$to[]  = "You@Your.org";
$to[]  = "Her@Your.org";
$to[]  = "Him@Your.org";



// IF THE DATA HAS BEEN POSTED
if (!empty($_POST['email']))
{
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $email      = clean_string($_POST["email"]);
    $name       = clean_string($_POST["name"]);
    $telephone  = clean_string($_POST["telephone"]);

    // CONSTRUCT THE MESSAGE THROUGH STRING CONCATENATION
    $content    = NULL;
    $content   .= "You have a New Query From $name" . PHP_EOL . PHP_EOL;
    $content   .= "Tel No: $telephone" . PHP_EOL;
    $content   .= "Email: $email" . PHP_EOL;

    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
    }
}


// A FORM TO TAKE CLIENT INPUT FOR THIS SCRIPT
$form = <<<ENDFORM
<form method="post">
Please enter your contact information
<br/>Email: <input name="email" />
<br/>Phone: <input name="telephone" />
<br/>Name:  <input name="name" />
<br/><input type="submit" />
</form>
ENDFORM;

echo $form;



// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    // IF MAGIC QUOTES IS ON, WE NEED TO REMOVE SLASHES
    $str = stripslashes($str);

    // REMOVE EXCESS WHITESPACE
    $rgx
    = '/'               // REGEX DELIMITER
    . '\s'              // MATCH THE WHITESPACE CHARACTER(S)
    . '\s+'             // MORE THAN ONE CONTIGUOUS INSTANCE OF WHITESPACE
    . '/'               // REGEX DELIMITER
    ;
    $str = preg_replace($rgx, ' ', $str);

    // REMOVE UNWANTED CHARACTERS
    $rgx
    = '/'               // REGEX DELIMITER
    . '['               // START OF A CHARACTER CLASS
    . '^'               // NEGATION - MATCH NONE OF THE CHARACTERS IN THIS CLASS
    . 'A-Z0-9&+:?_.- '  // CHARACTERS WE WANT TO KEEP
    . ']'               // END OF THE CHARACTER CLASS
    . '/'               // REGEX DELIMITER
    . 'i'               // CASE-INSENSITIVE
    ;
    $str = preg_replace($rgx, NULL, $str);

    return $str;
}

Open in new window

To all:

I woke up last night thinking "It's the from address, because villageairportvan.com does not exist (right now).

So I removed the $headers .= "From: info@villageairportvan.com\r\n"; & it worked.

I vaguely remember this happening another time.
Hmm... Probably tripped a spam filter because the FROM address was unroutable!