Search code examples
emailsendmailsendsendmessage

Why my html code also display when i send email?


hi dear this is my code to send email my message code is this

$to = $email;
$subject = "Activation";
$message = "Your activation key is this " .$key.'<br>'.' click here to activate your acount. <a href="activation.php?key='.$key.'">here</a>';
$from = "riaz_qadeer90@yahoo.com";
$headers = "From:" . $from;
if(mail($to,$subject,$message,$headers))
{
    echo "Check your email to activate your acount.";
}

The problem is this when i send email the whole message shown in my inbox with code. why it not show "Click" as an anchore.....


Solution

  • Mail isn't HTML format by default, so it's sending this as plain text. See Example #4 on the PHP page for mail() for sending HTML email. You need to specify the content type for the message headers:

    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    

    Note the .= operator being used here. You'd be appending this to your existing $headers value. So you'll also want to make sure your existing header is terminated properly:

    $headers = "From:" . $from . "\r\n";