简体   繁体   中英

PHP mail function not working

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 messages@theredline.com \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 turbocapitalist1987@yahoo.com. I got the email but the "from" part doesn't work . Yahoo says that my email was sent from "the@yahoo.com"

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

Thanks

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

$headers .= "From: The Red-line <messages@theredline.com> \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.

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

$headers .= "From: The Red-line <messages@theredline.com> \r\n";

Secondly, on Unix/Linux, you must have a working MTA (ie: 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.

我建议使用预构建的PHP电子邮件库,例如: http : //swiftmailer.org/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM