When I get mail, it comes from "unknown sender" and all headers in Gmail appear as a part of message text. What I am missing?


$to = "toaddress@domain.com";
$from = "fromaddress@domain.com";
$sub = "Subject";
$mes  "Message text";

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=utf-8\r\n";
$headers .= "To: ".$to." <".$to.">\r\n";
$headers .= "From: ".$from." <".$from.">\r\n";
$headers .= "Reply-To: ".$from." <".$from.">\r\n";
$headers .= "Return-Path: ".$from." <".$from.">\r\n";
mail($to, $sub, $mes, $headers);

Thanks.

    Hi,

    try:

    $to = "toaddress@domain.com"; 
    $from = "fromaddress@domain.com"; 
    $sub = "Subject"; 
    $mes  "Message text"; 
    
    $headers = "MIME-Version: 1.0\r\n"; 
    $headers .= "Content-type: text/plain; charset=utf-8\r\n"; 
    $headers .= "To: ".$to." <".$to.">\r\n"; 
    $headers .= "From: ".$from." <".$from.">\r\n"; 
    $headers .= "Reply-To: ".$from." <".$from.">\r\n"; 
    $headers .= "Return-Path: ".$from." <".$from.">\r\n"; 
    $headers .= "\r\n";
    mail($to, $sub, $mes, $headers); 
    
    

    Notice the $headers .= "\r\n";

    Cheers

      Also notice the missing '=' between $mes and its value.

      If that still doesn't work, try changing the "\r\n" line endings to just "\n".

        Thank you for your replies.

        I just mistyped. This line:
        $mes "Message text";
        was correct in my script:
        $mes = "Message text";

        Now I changed
        \r\n
        to
        \n
        as suggested bradgrafelman and the problem is gone.

        bradgrafelman, do you know why it doesn't work with \r\n ? I have seen many forum messages where people suggest to change \n to \r\n as I had before.

          It's a server-specific problem (more specifically, an application-specific problem).

          So, if you port this same script to a different server, it'll likely break. If you visit the manual page [man]function.mail[/man], you'll see why:

          Note: If messages are not received, try using a LF (\n) only. Some poor quality Unix mail transfer agents replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with » RFC 2822.

            Before I had the following lines in my script:

            mail($to, $sub, $mes, "From: \"".$from."\" <".$from.">\r\nReturn-Path: <".$from.">\r\nContent-Type: text/plain; charset=utf-8\r\n");
            

            In this case only the last line of headers appeared as a part of message text:

            Content-Type: text/plain; charset=utf-8
            
            Message text
            

            Then I changed headers to:

            $headers = "MIME-Version: 1.0\r\n";
            $headers .= "Content-type: text/plain; charset=utf-8\r\n";
            $headers .= "To: ".$to." <".$to.">\r\n";
            $headers .= "From: ".$from." <".$from.">\r\n";
            $headers .= "Reply-To: ".$from." <".$from.">\r\n";
            

            When I did it, all headers were visible as a part of message.

            When I added:

            $headers .= "Return-Path: ".$from." <".$from.">\r\n"; 
            

            all emails came from apache@myhostname ignoring "from" address.

            Note: If messages are not received, try using a LF (\n) only. Some poor quality Unix mail transfer agents replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with » RFC 2822.

            Messages are always received. The problem is the way how they are received.
            What does it mean "poor quality mail transfer agents"???

            Now when I have \n instead of \r\n, everything works fine, but I would like to fix this problem so that mail are delivered properly from any server and to any server.

              Well... without using other classes (e.g. PHPMailer) to handle the SMTP connection, you'd simply have to fix the MTA on your Unix server. I have no experience doing this, and if you're on a shared host this wouldn't even be your responsibility in the least.

                bradgrafelman wrote:

                It's a server-specific problem (more specifically, an application-specific problem).

                So, if you port this same script to a different server, it'll likely break. If you visit the manual page [man]function.mail[/man], you'll see why:

                True, in my script I had to change all the \n to \r\n...
                And add the empty line add the end of all the headers.

                Hope it works now mate!
                Cheers

                  Write a Reply...