Search code examples
phphtmlhtml-email

PHP Mail not sending from "FROM Address"


I do not know why it is not sending from "From" Address but from some "[email protected]". I was looking for any solution but i did not find any. Still it sends email from [email protected]. My code:

<?php

if (isset($_POST['name']) && isset($_POST['email']))
{

$name = $_POST['name'];
$email = $_POST['email'];
$from = $_POST['email'];
$to = '[email protected]';
$subject = 'Kontaktní formulář';
$body = '<html>
<body>
<p>Jméno: '.$name.'</p>
<p>Email: '.$email.'</p>
<p>Zpráva:<br/><br/>'.$_POST['comments'].'</p>
</body></html>';

$headers = "From: ".$email."\r\n"; 
$headers = "Reply-To: ".$email."\r\n";
$headers = "MIME-Version: 1.0" . "\r\n";

$headers ="Content-type:text/html;charset=UTF-8" . "\r\n";

$send = mail($to, $subject, $body, $headers);

} 
?>

Please, does anyone know where could be any mistake?


Solution

  • You are overriding headers assigning them to the same variable without concatenation. Look at example #5

    // To send HTML mail, the Content-type header must be set
    $headers = 'MIME-Version: 1.0\r\n';
    $headers .= 'Content-type: text/html; charset=UTF-8\r\n';
    
    // Additional headers
    $headers .= "From: $email\r\n";
    $headers .= "Reply-To: $email\r\n";
    

    [EDIT] Then as Pawel Szalucki suggested look at this post.

    $send = mail($to, $subject, $body, $headers, "-f $email");