Question

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="info@domain.com";
 $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 <info@domain.com>";

  $from = "Contact $name <info@domain.co.uk>";

  $host = "mail.exclusivehosting.net";
  $username = "info@domain.co.uk";
  $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> 
Was it helpful?

Solution 2

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',

);

OTHER TIPS

You can use the following method.

function sendMail($email, $subject, $message)
{
    $supportEmail = 'info@abc.com';
    $from = 'Abc';
    $msg  = $message;
    $from = str_replace(' ', '-', $from);
    $frm  = $from.' <'.$supportEmail.'>';
    preg_match("<(.*)@(.*\..*)>", $frm, $match);

    ///////////////////Headers/////////////////
    $hdr='';
    $hdr.='MIME-Version: 1.0'."\n";
    $hdr.='content-type: text/html; charset=iso-8859-1'."\n";
    $hdr.="From: {$frm}\n";
    $hdr.="Reply-To: {$frm}\n";
    $hdr.="Message-ID: <".time()."@{$match[2]}>\n";
    $hdr.='X-Mailer: PHP v'.phpversion();
    $x=@mail($email, $subject, $msg, $hdr);
    if($x==0)
    {
        $email=str_replace('@','\@', $email);
        $hdr=str_replace('@','\@',$hdr);
        $x=@mail($email, $subject, $msg, $hdr);
    }
    return $x;
}

you can use:

$message_new=html_entity_decode($mesage);

and you can pass $message_new variable to mail funciton for this you can getting proper output. Or refer this URL:

http://php.net/manual/en/function.html-entity-decode.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top