PHP send HTML email, The complete php mail sending script

The last post by me was about a very easy way to send an email with php. But as it is simple, it will only send text email, not html.

If you want a complete mail sending script, that send in html, this is what you have to use:

<?php
//here the receiver of the email
$to = 'receiver@example.com';
//here 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: myemail@mysite.com\r\nReply-To: myemail@mysite.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.
$message ='
--PHP-alt-'.$random_hash.'
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt-'.$random_hash.'--';

//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";
?>

The script is with comments, to tell you what exactly each instruction does :)

So as you see, we added:
Content-Type: text/html; charset="iso-8859-1"
to the message body ($message), this way the mail client of the receiver will know that the email is in html.

There are also few other changements that are nor really very necessary like the boundary string, but it is better to use them :)

9 comments:

Derick said...

Gosh, there's a lot of helpful information above!
free mmorpg games online | Hotels In Duluth MN | what is a deductible for car insurance

Alexander said...

Gosh, there is so much useful data above!
RHUBARB - BE CUED BEEF RIBS

Joseph Ndungu said...

Great post. See a simple and working demo plus tutorial at Jose's Blog

rize wordpress said...

Rize PHP practice consists of Dedicated PHP Developers certified in related field from reputed universities and colleges.

RS Gold said...


Worth it to read report. Especially. your appears utilized to offer the head. I would not have noticed the idea by myself. Must examine carefully outdated photographs to see this selection. buy Diablo 3 gold



Cheap GW2 Gold

rscredits said...

I have read your articles for a long time, and i think you really did a good jod.I have formed a habbit to read your articles.And now i hope for more good jobs. rs gold , runescape gold

Guild wars 2 gold said...

Very worthwhile report. Especially. the actual holders accustomed to support the go. I would not have noticed that alone. Will have to look carefully at previous photos to determine this feature diablo 3 gold

guildwars said...

To purchase gw2 gold online for game improvement, try to order on the professional Guild Wars 2 stores like gwsafe. All cheap guild wars 2 gold on our site is manually obtained by high-level proficient GW2 gamers who are good at farming. So, our gold online is totally safe and legit for you to directly use it in game, and will not get your account in any risk! Picking up our guild wars 2 gold for sale, you never need to worry about your account security, what you just need to do is just focusing on using the gold in game to fulfill your higher-level challenge!

guildwars said...

I you need more game currency to level your account up, or boost your character, you can come to buy neverwinter diamond on safewow! All our neverwinter astral diamond are safely and decently manual, and you account will encounter no trouble with them! Plus, they are on sale at the lowest online price on our site, and our fast delivery will arrive within 15 minters after your order!