PHP: How to send HTML email?

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


12 points

Hello,

I am writing a php code to send email in HTML? Following is the code that I have written. It prints HTML tags as well instead of executing them in HTML mail.


<?php
$txtMsg 
'<b><h1> This is HTML mail</h1></b> This is a HTML body.';

ini_set "SMTP""xy.domain.com" ); 
$mail_to'someone@domain.com';
$mail_from='someone@domain.com';
$mail_sub='PHP HTML MAIL';
$mail_mesg=$txtMsg;

//Check for success/failure of delivery 
if(mail($mail_to,$mail_sub,$mail_mesg,"From: $mail_from"))
echo 
"<br><br>Email Successfully Sent!";
else
echo 
"<br><br>Error Sending Email!";
}

?>

Please help



11 points

You need to specify MIME version and content type in your header field of email to be able to send email as HTML as follows:

<?php
$header 
= array(
    
"From: $mail_from",
    
"MIME-Version: 1.0",
    
"Content-Type: text/html"
);
mail($mail_to$mail_sub$mail_mesgimplode("\r\n"$header))
?>

This should work.

Anonymous's picture
Created by Anonymous

Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.