Page 1 of 1

Mail() sends email but fails when I try to add headers!

Posted: Mon Mar 28, 2005 8:03 pm
by johnnybt

Code: Select all

<?

//Declare the variables
$subject = "Team Submission";
$message = 
"Name: $name
Teamname: $teamname
Email: $email
Tiebreaker: $tiebreaker 
Team: $team";

//contents of form

$name = $_POST['name'];
$teamname= $_POST['teamname'];
$email =    $_POST['email'];
$email =   $_POST['tiebreaker'];
$team =     $_POST['team'];
$headers .= "From: $name <$email>\r\n";
$headers .= "Cc: $email\r\n";





//mail() function sends the mail
mail("me@email.com", $subject, $message, $headers);

?>
if i just take out the headers and the $headers in the mail line it works! confused...i'm thinking the \r\n are screwing it up, and i don't know much about what they do anyway.

Posted: Mon Mar 28, 2005 8:06 pm
by John Cartwright
try searching our forums.. we've discussed how to send the proper headers several times. :roll:

Posted: Mon Mar 28, 2005 8:56 pm
by johnnybt
To be honest, I'm a complete novice to PHP and programming and really will never be more than that. I need help with a (probably) simple problem. I just DID search and couldn't find an answer to MY question. If you don't want to help that's fine, I was just thinking maybe SOMEONE would.

I also searched to see if I could find out what \r and \n do, but no luck there either.

Posted: Mon Mar 28, 2005 9:07 pm
by feyd
\r and \n are line-feed and carriage return, respectively, if I remember correctly. They tell the mail system a new line is next.

Posted: Mon Mar 28, 2005 9:34 pm
by johnnybt
thanks. can you see any reason why adding the two headers like i did above would cause the function to not send any mail? i'm really frustrated.

Re: Mail() sends email but fails when I try to add headers!

Posted: Mon Mar 28, 2005 11:26 pm
by SystemWisdom
johnnybt wrote:

Code: Select all

$headers .= &quote;From: $name &lt;$email&gt;\r\n&quote;;
$headers .= &quote;Cc: $email\r\n&quote;;
The $headers variable has not been initialized to a default value, and you are concatenating right away...

Try:

Code: Select all

$headers = ''; 
$headers .= &quote;From: $name &lt;$email&gt;\r\n&quote;;
$headers .= &quote;Cc: $email\r\n&quote;;
Or just:

Code: Select all

$headers = &quote;From: $name &lt;$email&gt;\r\n&quote;;
$headers .= &quote;Cc: $email\r\n&quote;;
Also, depending on the OS of the server, the Line-endings may differ.. What I tend to do (I dont know if this is a best-practices method or not) is always use \n in my strings, and then pass them to a FixEOL() function, to correct the line-endings depending on the OS, like:

Code: Select all

function FixEOL( &amp;$szData )
{
    if( strtoupper( substr( PHP_OS, 0, 3 ) == 'WIN' ) )
        $szData = str_replace( &quote;\n&quote;, &quote;\r\n&quote;, $szData );
    else if( strtoupper( substr( PHP_OS, 0, 3 ) == 'MAC' ) )
        $szData = str_replace( &quote;\n&quote;, &quote;\r&quote;, $szData );
    return;
}
Anyway, I hope that helps!

Posted: Mon Mar 28, 2005 11:37 pm
by johnnybt
i knew that the header not being initialized was a problem, i was just copy and pasting my code from last year, which for some reason worked. i've tried all of your suggestions with still no luck. i've emailed the server support to see if they have any idea why it's not working.

on an unrelated (well, who knows) note, when i try to do:

Code: Select all

$recipient = "me@myemail.com";

mail($recipient,...)
i don't get mail. only when i do

Code: Select all

mail("me@myemail.com",....)
weird.