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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
johnnybt
Forum Newbie
Posts: 7
Joined: Sun Mar 27, 2005 9:42 pm

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

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

try searching our forums.. we've discussed how to send the proper headers several times. :roll:
johnnybt
Forum Newbie
Posts: 7
Joined: Sun Mar 27, 2005 9:42 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
johnnybt
Forum Newbie
Posts: 7
Joined: Sun Mar 27, 2005 9:42 pm

Post 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.
User avatar
SystemWisdom
Forum Commoner
Posts: 69
Joined: Sat Mar 26, 2005 5:54 pm
Location: A Canadian South of the 49th Parallel

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

Post 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!
johnnybt
Forum Newbie
Posts: 7
Joined: Sun Mar 27, 2005 9:42 pm

Post 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.
Post Reply