By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,939 Members | 1,602 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,939 IT Pros & Developers. It's quick & easy.

How to send a html by php mail function

sanjay123456
100+
P: 125
Dear friends,

[PHP]

<?php
//define the receiver of the email
$to = 'sanjay.mit@gmail.com';
//define the subject of the email
$subject = 'Test HTML email';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?><?php echo $random_hash; ?>


<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
</body>


<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
echo "sending a mail in html in php";
?>
[/PHP]



I would like to following output

Hello World!
This is something with HTML formatting.


but its give the following output
[HTML]
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<body>

<h2>Hello World!</h2>

<p>This is something with <b>HTML</b> formatting.</p>

</body>
[/HTML]

so anyone tell where i am doing wrong in this code ??????

thanks in advance !!


sanjay
Jan 17 '08 #1
Share this Question
Share on Google+
3 Replies


Markus
Expert 5K+
P: 6,050
You need to send this with your headers

[php]
$headers .= "Content-type: text/html; charset=iso-8859-1";
[/php]
Jan 17 '08 #2

sanjay123456
100+
P: 125
Dear guru ,


Its also not working here .

so what can i do for this problem ????
Jan 17 '08 #3

stepterr
100+
P: 157
Dear guru ,


Its also not working here .

so what can i do for this problem ????

Take a look at this thread. I posted an example of a very basic way to send html within an email using the PHP mail function. Maybe that will help you as well
Jan 17 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.