Search code examples
phpemailemail-headers

$headers not working on centos


I have a simple mail function:

 $to   = 'someone@somedomain.com';
 $cc   = 'someother@somedomain.com';
 $bcc  = 'someotherother@somedomain.com';
 $sub  = 'Some Subject';
 $body = $html;

 $headers[] = "MIME-Version: 1.0";
 $headers[] = "Content-type: text/html; charset=iso-8859-1";
 $headers[] = "From: Fancy Name <from@email.com>";
 $headers[] = "Reply-To: Fancy Name <from@email.com>";
 $headers[] = 'CC: '. $cc;
 $headers[] = 'BCC: '. $bcc;

 mail($to, $sub, $body, implode("\n", $headers));

this works on windows but not on CentOS - the logs return "Envelope From address apache@host is't allowed" (or something similar).

Looked into that and as a 5th param you can pass -f from@email.com and this does work. But it doesn't show as Fancy Name, it shows the email address - how can I either get headers to work properly or pass a name to the -f param?

Thanks


Solution

  • Found a solution:

    do the headers as your would and in the 5th param do -f email@domain -F "Sender Name" e.g.

     $to   = 'someone@somedomain.com';
     $cc   = 'someother@somedomain.com';
     $bcc  = 'someotherother@somedomain.com';
     $sub  = 'Some Subject';
     $body = $html;
    
     $headers[] = "MIME-Version: 1.0";
     $headers[] = "Content-type: text/html; charset=iso-8859-1";
     $headers[] = "From: Fancy Name <from@email.com>";
     $headers[] = "Reply-To: Fancy Name <from@email.com>";
     $headers[] = 'CC: '. $cc;
     $headers[] = 'BCC: '. $bcc;
    
     mail($to, $sub, $body, implode("\n", $headers), '-f from@email.com -F "Fancy Name"');