phphtmlssmtp

Send HTML mails using PHP


Possible Duplicate:
Sending HTML email from PHP

I am sending emails using php through my gmail account. And I am using ssmtp other than sendmail. I am using following php code to send the mail.

$to = "mymail@gmail.com";
$from = "mymail@gmail.com";
$subject = "Testing mail";
$header  = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset: utf8\r\n";     
$headers = "From:" . $from;
$message = "<html><body><head></head>";
$message .= "<h1>Hello, World!</h1>";
$message .= "</body></html>";

if(mail($to,$subject,$message,$headers)){
   echo "Mail Sent.";
} else {
   echo 'failed';
}

But I am getting a mail like below

<html><body><head></head><h1>Hello, World!</h1></body></html>

What may be the reason for it? I am using ssmtp MTA in Ubuntu.


Solution

  • use this

    <?php
    $to = "mymail@gmail.com";
                    $from = "mymail@gmail.com";
                    $subject = "Testing mail";
                    $header  = "MIME-Version: 1.0\r\n";
    
            $header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    
                    $header .= "From:" . $from;
    
                    $message = "<html><body><head></head>";
                    $message .= "<h1>Hello, World!</h1>";
                    $message .= "</body></html>";
    
                    if(mail($to,$subject,$message,$headers)){
                    echo "Mail Sent.";
                    } else {
                    echo 'failed';
                    }
    ?>