Page 1 of 1

MAIL FUNCTION

Posted: Thu Mar 16, 2006 2:09 pm
by trim9001
Pls Help! I was trying to create a application with the "mail()" function. The prgram gives an error on the mail() function. I have php v5 and according to the php.net webpage, the function should work but it dosen't. PLS HELP!

HERE IS THE CODE:
<form action="mail.php" method="post">
<input type="text" name="text">
<br />
<br />
<label>
<textarea name="textarea"></textarea>
<br />
</label>
<br />
<input type="submit" name="submit" value="Submit">
</form><br />

Code: Select all

$to = "trim9001@yahoo.com";
$subject = "Results from your Request Info form"; 
$headers = "From: My Site"; 
$forward = 0; 
$date = date ("l, F jS, Y"); 
$time = date ("h:i A"); 
$msg = "Below is the result of your feedback form. It was submitted on $date at $time.\n\n"; 
if ($_SERVER['REQUEST_METHOD'] == "POST") {
    foreach ($_POST as $key => $value) { 
        $msg .= ucfirst ($key) ." : ". $value . "\n"; 
    }
}
else {
    foreach ($_GET as $key => $value) { 
        $msg .= ucfirst ($key) ." : ". $value . "\n"; 
    }
}
mail($to, $subject, $msg, $headers); 
if ($forward == 1) { 
    header ("Location:$location"); 
} 
else { 
    echo "Thank you for submitting our form. We will get back to you as soon as possible."; 
}
ERROR:
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\Program Files\Apache Group\Apache2\htdocs\mail.php on line 39

Posted: Thu Mar 16, 2006 2:12 pm
by Burrito
do you have a mail server set up on your server?

I'd bet no given the error.

try modifying your php.ini file to point to a valid SMTP server....

Posted: Thu Mar 16, 2006 2:33 pm
by gchrome
Burrito is right, this is a common problem.

If you don't have access to the php.ini to make the change to the default SMTP server or if you need to have more detailed configuration options (ex. sending in both html and plain text) you should use the PHPMailer class available at http://phpmailer.sourceforge.net/

I have used it in many applications over the years and it is very handy for getting around the limitations of the regular php mail() function.