Search code examples
phpmass-emails

How to send html emails php


i am using following code in order to send html emails through php, please check my below code where i am doing wrong. its send emails but not in html format, i have tried so many examples avaiable on net but unable to get emails in html formats. please help me in this regard.

 require_once "Mail.php";
 $name="Me";
 $mail_from="[email protected]";
 $subject=$mail_from." Emails";

 $message = " 
 <html> 
  <body bgcolor=#DDDDDD> 
   YOUR HTML EMAIL
  </body> 
 </html> 
 "; 


 $From="from: $name <$mail_from>"; 

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


  $to = "Shop <[email protected]>";

  $from = "Contact $name <[email protected]>";

  $host = "mail.exclusivehosting.net";
  $username = "[email protected]";
  $password = "(hello)";

  $headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
  $smtp = Mail::factory('smtp',
  array ('host' => $host,
  'auth' => true,
  'username' => $username,
  'password' => $password));

  $mail = $smtp->send($to, $headers, $message);


  if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
  } else {
  //echo("<p>Message successfully sent!</p>");
  echo'<table width="800" border="0" align="center" >
  <td>';
  echo "We've recived your contact information, we will get back soon!";
echo'</td>
</table>';
  }

as out i am getting following email

  <html> 
  <body bgcolor=#DDDDDD> 
  YOUR HTML EMAIL
  </body> 
  </html> 

Solution

  • You're rewriting your headers at the bottom of the script from what I can see.

    Add the content type headers into the array.

    $headers = array (
        'From' => $from,
        'To' => $to,
        'Subject' => $subject,
        'Content-Type' => 'text/html; charset=iso-8859-1',
        'MIME-Version' => '1.0',
    

    );