Question

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.

Was it helpful?

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';
                }
?>

OTHER TIPS

Try using the following in your code

$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

This link will be most helpful

Sending HTML email with PHP

You are using header (singular) and headers (plural) indistinctly. The correct way is using plural for the headers:

$headers  = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

This is a good reading: http://css-tricks.com/sending-nice-html-email-with-php/

Thats because PHP does not make new lines by it self. You can use echo "Lalala \n" to make new lines.

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