Search code examples
phphtml-email

HTML tags are displayed in mail


I am trying to send a text by mail in php. The message is placed inside a HTML tag. While sending, the mail part works but the text I receive has HTML tags in it.

Here is the code for better understanding of my issue:

$car = "1223455667";
$to = "[email protected]";
$subject = 'Test Mail';
$message = '<p>' . $car . '</p>';

$from = '[email protected]';

if(mail($to, $subject, $message )){
    echo 'success';
}else{
    echo 'Unable to send the email. Please try again.';
}

Output I receive in mail:

enter image description here

I expect the output like this: 1223455667


Solution

  • Pass Headers in your mail

    $headers = "MIME-Version: 1.0" . "\r\n"; 
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
    
    if(mail($to, $subject, $message, $headers )){
        echo 'success';
    }else{
        echo 'Unable to send the email. Please try again.';
    }