• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

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

0
Richard Korts
Asked:
Richard Korts
  • 5
  • 4
  • 2
  • +1
2 Solutions
 
Ray PaseurCommented:
What do you mean by "fail?"
0
 
Ray PaseurCommented:
The php function mail() returns a boolean.  See http://php.net/manual/en/function.mail.php

You can't expect much when you echo a boolean.  But you can test it with if($m) and see what you got back.
0
 
Richard KortsAuthor Commented:
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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Scott MadeiraCommented:
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);

0
 
Richard KortsAuthor Commented:
smadeira:

I put that there & did it again.

Nothing
 
You can try it at http://s385518869.onlinehome.us/contact.php
0
 
Sudaraka WijesingheWeb Application ProgrammerCommented:
First thing would be to verify you have some mail software installed/configured on the server such as sendmail and it is working correctly.
And then check the mail log to see if request from mail() reaches the mail program, if not check the email parameters on your PHP configuration.

Try a simple script such as:
<?php
error_reporting(E_ALL);
$b_return = mail('your email address', 'Testing', 'This is a test');
echo $b_return ? 'Success':'Fail';

Open in new window

P.S. Also note mail() doesn't usually fail due to data error.
0
 
Richard KortsAuthor Commented:
sudaraka:

That worked, I got the email.
0
 
Sudaraka WijesingheWeb Application ProgrammerCommented:
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?
0
 
Ray PaseurCommented:
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

0
 
Ray PaseurCommented:
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

0
 
Richard KortsAuthor Commented:
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.
0
 
Ray PaseurCommented:
Hmm... Probably tripped a spam filter because the FROM address was unroutable!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now