tags:

views:

48

answers:

3

Hi everyone,

I have a php script that sends an email. It looks like this:

<?php
 // subject
 $subject = "$first_name $last_name has sent you a message on The Red-line";

 // message
 $message = "<html>
    <head>
     <title>
      The Red-line
     </title>
    </head>
    <body>
     <p>
      Hi $war_first, 
     </p> <br />

      <p>
       $first_name $last_name has sent you a message on the Red-line. To view your message, please login to <a href='www.thered-line.com'>the Red-line.</a>
      </p> <br />

      <p>
       Sincerely,
      </p> <br />

      <p>
       The Red-line Operator
      </p> 
    </body>
    </html>";

//  To send HTML mail, the Content-type header must be set
 $headers    =  'MIME-Version: 1.0' . "\r\n";
 $headers   .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

//  Additional headers
 $headers   .=  "From: The Red-line [email protected] \r\n";
 $headers   .=  "To: $war_first $last_war <$war_mail>\r\n";

//  Mail it
 mail($war_mail, $subject, $message, $headers);
?>

When I was testing this out on the remote server, I used this script to send an email to my inbox at [email protected]. I got the email but the "from" part doesn't work . Yahoo says that my email was sent from "[email protected]"

Does anybody have a clue as to why this isn't working?

Thanks

+2  A: 

For your From header, enclose the actual address with < >:

$headers .= "From: The Red-line <[email protected]> \r\n";

This is the correct format to use when you have both a display name and an email address. I'm guessing Yahoo was interpreting the first word "The" as the entire email address, and provided a default (yahoo.com) for the domain.

willell
+1  A: 

The <> seems to be your problem, it should be:

$headers .= "From: The Red-line <[email protected]> \r\n";

Secondly, on Unix/Linux, you must have a working MTA (i.e.: sendmail, postfix, etc.) configured on your server, either to relay mail directly, or through a Smart Host. If you're on Windows, you must have a SMTP server configured in php.ini.

netcoder
Thanks a lot, man. It's always these minor little syntax errors that catch me of guard lol.
Lance Newman
A: 

I would recommend using pre-built PHP mailing class/functions such as http://www.phpclasses.org/package/264-PHP-Full-featured-email-transfer-class-for-PHP.html

Petah