Forum Moderators: coopster

Message Too Old, No Replies

PHP mail() function not working - can you help?

         

mr_nabo

5:55 pm on Jan 13, 2009 (gmt 0)

10+ Year Member



Hi,

the following code I have doesn't seem to be sending out emails. Can anyone help me find out why? Also, does this script look secure as it is?

Thanks


<?php

// ***** Preparing the email *****

// Main recipient

$to = 'Mr Mysterious <mrm@example.com>';

// received variables, strip any bull#*$! from it all
$orgName = strip_tags($_POST['borgName']);
$orgName = htmlspecialchars($_POST['borgName']);
$adLine1 = strip_tags($_POST['madLine1']);
$adLine1 = htmlspecialchars($_POST['madLine1']);
$adLine2 = strip_tags($_POST['madLine2']);
$adLine2 = htmlspecialchars($_POST['madLine2']);
$postcode = strip_tags($_POST['postcode']);
$postcode = htmlspecialchars($_POST['postcode']);

// Strip tags from entries
$comments = strip_tags($_POST['comments']);
// No naughty weird HTML characters
$comments = htmlspecialchars($_POST['comments']);
// Convert Windows (\r\n) to Unix (\n)
$comments = ereg_replace("\r\n", "\n", $_POST['comments']);
// Convert Macintosh (\r) to Unix (\n)
$comments = ereg_replace("\r", "\n", $_POST['comments']);
// Handle paragraphs - note the </p> before <p>
$comments = ereg_replace("\n\n", "</p><p>", $_POST['comments']);
// Handle line breaks
$comments = ereg_replace("\n", "<br />", $_POST['comments']);

// subject
$subject = '[form]';

// message
$message .= '[form] New signup' . "\n\n";
$message .= 'Borg name: ' . $borgName . "\n";
$message .= '1st line of address: ' . $madLine1 . "\n";
$message .= '2nd line of address: ' . $madLine2 . "\n";
$message .= 'Comments: ' . stripslashes($comments) . '</p>';

$headers .= 'From: [form]';

// Stop the form being used from an external URL
// Get the referring URL
$referer = $_SERVER['HTTP_REFERER'];

// Get the URL of this page
$this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];

// If referring URL and URL of this page match
// and if there's data coming from a form then do the mailing
if ($_POST & ($referer !== $this_url)) {
echo '<h1>Naughty person, don\'t try and spam people using this form!</h1>';
exit;
} else {
mail($to, $subject, $message, $headers);
}

?>

[edited by: eelixduppy at 6:57 pm (utc) on Jan. 13, 2009]
[edit reason] exemplified [/edit]

cameraman

7:47 pm on Jan 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might want to set error_level(E_ALL) until you get the script debugged - it should be generating several warnings. These lines:
$message .= '[form] New signup' . "\n\n";
$message .= 'Borg name: ' . $borgName . "\n";
$message .= '1st line of address: ' . $madLine1 . "\n";
$message .= '2nd line of address: ' . $madLine2 . "\n";

The first one, $message hasn't been defined for the concatenation, and the other three because the variables mentioned in them haven't been defined either. Also this line:
$headers .= 'From: [form]';

You haven't defined $headers for the concatenation either, and it's possible that you need to supply a valid email address (format anyway) to add a From header - that may be what's actually holding up the send. Try mail($to,$subject,$message); to see if that works - setting the error level should also reveal if the mail() function is returning a warning about the From header.

mr_nabo

9:57 am on Jan 14, 2009 (gmt 0)

10+ Year Member



Thanks for this, I got it working after making the changes you recommended. Must have been a late night last night, there were quite a few errors!

Thanks