Search code examples
phpemailemail-headers

"From" in php mail $headers not passing


I have a mail script that was working fine and I can't figure out what changed that's not causing the "from" to pass.

I have this code in a mail function

$to = ($_POST['email']);
$subject = 'Welcome to the Team!';
$url = 'mydomain.com'; 
$headers = "From: info@mydomain.com\r\n";
$headers = "BCC: me@mydomain.com\r\n";
$headers = "MIME-Version: 1.0\r\n";
$headers = "Content-Type: text/html; charset=ISO-8859-1\r\n";
...
mail($to, $subject, $message, $headers);

The problem is, when the email comes through, the "from" looks like this:

From: (mydomain)@(someletters&numbers).shr.phx3.(myhost).net

instead of ...

From: info@mydomain.com

What's up?


UPDATE WITH FULL CODE

...
if(count($errors) == 0) {
$to = 'me@mydomain.com';
$subject = 'Subject';
$headers = "From: me@ mydomain.com\r\n";
$headers = "BCC: someonelese@ mydomain.com\r\n";
$headers = "MIME-Version: 1.0\r\n";
$headers = "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>…Email Message 1…</body></html>";
mail($to, $subject, $message, $headers);
}
if(count($errors) == 0) {
$to = ($_POST['email']);
$subject = 'Subject';
$url = 'mydomain.caom'; 
$headers = "From: me@ mydomain.com\r\n";
$headers = "BCC: someonelese@ mydomain.com\r\n";
$headers = "MIME-Version: 1.0\r\n";
$headers = "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>…Email Message 2…</body></html>";
mail($to, $subject, $message, $headers);
echo '<META HTTP-EQUIV=Refresh CONTENT="1; URL='.$url.'">';
}

Solution

  • Your headers are broken and need to be concatenated using a dot after the first declaration:

    $headers = "From: info@mydomain.com\r\n";
    $headers .= "BCC: me@mydomain.com\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    

    Consult the manual:


    Edit:

    Rename your second set of headers to $headers, for example:

    if(count($errors) == 0) {
    $to = 'me@mydomain.com';
    $subject = 'Subject';
    $headers = "From: me@ mydomain.com\r\n";
    $headers .= "BCC: someonelese@ mydomain.com\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $message .= "<html><body>…Email Message 1…</body></html>";
    mail($to, $subject, $message, $headers);
    }
    if(count($errors) == 0) {
    $to = ($_POST['email']);
    $subject = 'Subject';
    $url = 'mydomain.caom'; 
    $headers2 = "From: me@ mydomain.com\r\n";
    $headers2 .= "BCC: someonelese@ mydomain.com\r\n";
    $headers2 .= "MIME-Version: 1.0\r\n";
    $headers2 .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $message = "<html><body>…Email Message 2…</body></html>";
    mail($to, $subject, $message, $headers2);
    echo '<META HTTP-EQUIV=Refresh CONTENT="1; URL='.$url.'">';
    }