Insert HTML Into Email?


I have password reminder system however I am unable insert HTML to it. For example I would like to place a link into the email. However as I add commas it creates an error. Can anyone advise how I can inser HTML into the code. Currently it just prints the code because it has a single comma.

        $headers4="";  // Change this address within quotes to your address
$headers.="Reply-to: $headers4\
$headers .= "From: $headers4\
$headers .= "Errors-to: $headers4\
//$headers = "Content-Type: text/html; charset=iso-8859-1\
".$headers;// for html mail

// mail funciton will return true if it is successful
 if(mail("$em"," - Reset Password Request","This is in response to your request for login details at \
Hello $row->firstname $row->surname \

 <a href=''  rel='nofollow' >
<img src='/images/logo.png' alt=''/>
</a> \

<a href=''>Click Here</a>  \

Password: $row->password \
 Thank You \
 Site Admin","$headers"))
{echo "<center><b>THANK YOU</b> <br>Your password is posted to your email address .
Please check your email account. </center>";}

else{// there is a system problem in sending mail
 echo " <center>There is a problem in sending login details to your address.
Please contact Site Admin: . <br><br><input type='button' value='Retry' onClick='history.go(-1)'></center>";}

	else {// Validation failed so show the error message
echo "<center>$msg
<br><br><input type='button' value='Retry' onClick='history.go(-1)'></center>";}

Hi justlukeyou,

The above code snippet you have posted does have some clear indications as to why your HTML is showing up as plain text, the content-type header currently is commented out in the above code there for its been sent as Content-type: text/plain. To fix this you would simply un-comment that line but another issue would arise by doing that as the line begins with just $header = which would overwrite the previous headers set therefor you would need to change it to read $headers .= which would extend the previous header lines.

Now I’m hoping you understood all that so we can move onto the bulk of the issues which is the un-readable code, at the moment its hard to see where the mail function ends and what is doing what within it, to clean things up I personally like to set everything in variables and then call them as its far more readable and easier to maintain which as a developer should be of your top priorities during development. On that same note you will notice that the above code also has two else statements which would never work as it would always only ever hit one of them rather then both, see the below which is a cleaned up example of your above snippet which would need some modifications on your part but overall should work without any issues.

// Some kind of validation here
$validationPassed  = true;
$validationMessage = '';

if ($validationPassed) {
    $from    = "";                    // Change this address within quotes to your address
    $to      = "";                // The email address of the recepient
    $subject = " - Reset Password Request"; // The subject for the email address

    $message  = "<a href=''  rel='nofollow'><img src='/images/logo.png' alt=''/></a>\
    $message .= "This is in response to your request for login details at\
    $message .= "Hello $row->firstname $row->surname\
    $message .= "<a href=''>Click Here</a>\
    $message .= "Password: $row->password\
    $message .= "Thank You\
Site Admin";

    $headers  = "Reply-to: $from\
    $headers .= "From: $form\
    $headers .= "Errors-to: $form\
    $headers .= "Content-Type: text/html; charset=iso-8859-1\
" . $headers;

     if (mail($to, $subject, $message, $headers)) {
        echo "<div align='center'><strong>THANK YOU</strong><br />Your password is posted to your email address, please check your inbox/spam box.</div>";
    } else{
        echo "<div align='center'>There is a problem in sending login details to your address, please contact site admin.<br /><br /><input type='button' value='Retry' onclick='history.go(-1)'></div>";}
} else {
    echo "<div align='center'>$validationMessage<br /><br /><input type='button' value='Retry' onClick='history.go(-1)'></div>";

This is the Working Code for Parsing HTML in Email.