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:

//here the receiver of the email
$to = '';
//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:\r\nReply-To:";
//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 ='
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>


//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 :)


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!

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!

fifa coins online said...

Many thanks greatly with regard to discussing this unique info. I'd certainly such as.

fut coins
league of legends boosting
buy fifa coins
fifa ut coins

Divya Dheen said...

Thanks for the great information in your blog PHP Training in Chennai

geethu said...

Thanks for sharing this unique and informative content which provided me the required information.
PHP Training in Chennai | PHP Course in Chennai | FITA Velachery

WT Labs said...

Sounds like a very good idea. You will have heaps of subscriber for the ones do that. PHP Training in Jalandhar

deeksha said...

this php concept is really nice and innovative thanks for sharing thise information , really i got more knowledge about this concept.

php Training in Chennai

mary Brown said...

I have read your blog its very attractive and impressive. I like it your blog.

PHP Training in Chennai | Online PHP Training

PHP Training in Chennai | Online PHP Training

Anonymous said...

Nice post . Thanks for the shearing valuable information. I really fiend this type blog. Special thanks to writer. web designing jalandhar

Sanjana E said...

Glad to see your explanation. thanks for the update
Php Training institute in velachery

Xplorant Seo said...

Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

iOS Training in Chennai

jhansi joe said...

It's really useful post for the beginners, who willing to learn PHP .
PHP Training in Chennai|PHP Course in Chennai|PHP Training

meerja maqbul baig said...

Thnaks for providing neat content.. I am very happy as i found thiblog. i found your site in google search while searching for my web designing companies in karimnagar website.. Its nice thanks...



It's really nice to read this article.... Thanks for sharing this information....
HTML training in chennai

Lucky Srivastava said...

Hey Very nice Blog,Thanks For Sharing..

Best Php industrial training in Lucknow
best summer training in lucknow
Embedded system training in lucknow
Best Industrial training company in lucknow
best Php training center

Ramya Krishnan said...

Nice one, Thanks for the valuable information.
PHP Training in Chennai

varshini devi said...

I really enjoyed while reading your article, the information you have delivered in this post was damn good. Keep sharing your post with efficient news.
PHP Training in Chennai|PHP Course in Chennai

ranasing rajkumar said...

very interesting blog.This will help lot of user to update their knowledge.Thanks for sharing.
White Label Website Builder

kokila zenrays said...

Thanks for sharing this post. good information about php. Best PHP training in Bangalore

nanadhini said...

thanks for sharing . very useful information. keep sharing
PHP Training in Chennai

Lynda D. Harris said...

In this article I am will address a standout amongst the most much of the time made inquiries about email advertising. How frequently would it be advisable for me to send messages to my rundown? This is without a doubt a critical inquiry on the grounds that on the off chance that you fail to understand the situation you will either have many individuals withdrawing or you will have low change rates with regards to advancing your items. See More

King Host said...

This “php ustad” tutorial blog is designed for beginners – to learn php at home step by step using simple programming examples

raj kumar said...

Super information about PHP
Thanks for helping me in improving of knowledge on PHP
Please do sharing on...

JamesWilliam said...

Informative blog and it was up to the point describing the information very effectively. Thanks to blog author for wonderful and informative post...
PHP developers in chennai | PHP developers in India

Ancy merina said...

I was looking for such a blog that can provide me complete information about the web development services. Website designing is a process that requires expert hands. Any business planning to have a website created should seek the services of high-end website designing agencies.
Website Design and Development Services in Bangalore | website design companies in bangalore

Mahesh said...

Really very informative and creative contents. This concept is a good way to enhance the knowledge.thanks for sharing. please keep it up
HTML Training in Gurgaon

Online Sap Trainers said...

Nice blog post!!! Very informative and appreciable . keep posting

Best sap institute in India ,SAP online training & sap training courses
SAP ABAP on HANA Online Training
sap hana online training
sap S/4 HANA training online
SAP UI5 Online Training in India
Learn SAP online
sap fiori training online
sap fico training online


Divya M said...

I am really happy with your blog because your article is very unique and powerful for new reader.
Click here:
selenium training in chennai
selenium training in bangalore
selenium training in Pune
selenium training in pune
Selenium Online Training

Richa T said...

I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
Java training in Bangalore | Java training in Marathahalli | Java training in Bangalore | Java training in Btm layout

Java training in Bangalore | Java training in Marathahalli | Java training in Bangalore | Java training in Btm layout

Naga Manickam said...

Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
Data Science course in Chennai | Best Data Science course in Chennai | Data science course in bangalore | Best Data Science course in Bangalore

Data science course in pune | Data Science Course institute in Pune | Data science online course | Online Data Science certification course-Gangboard

Data Science Interview questions and answers

gkr ragini said...

Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
excel advanced excel training in bangalore | Devops Training in Chennai

john jersy said...

Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....
python training institute in marathahalli
python training institute in btm
Python training course in Chennai

sathya said...

Well done! Pleasant post! This truly helps me to discover the solutions for my inquiry. Trusting, that you will keep posting articles having heaps of valuable data. You're the best! 

angularjs interview questions and answers

angularjs Training in bangalore

angularjs Training in bangalore

angularjs Training in chennai

automation anywhere online Training

Dharani M said...

Good information

java training in Marathahalli

spring training in Marathahalli

java training institute in Marathahalli

spring and hibernate training in Marathahalli

mounika said...

Nice post..

python django training in BTM

python training centers in BTM

python scripting classes in BTM

python certification course in BTM

python training courses in BTM

python institutes in BTM

python training in btm

python course in btm

best python training institute in btm

mounika said...

Nice post..
DOT NET training in btm

dot net training institute in btm

dot net course in btm

best dot net training institute in btm

Anexas Europe said...

Thanks for your great and helpful presentation I like your good service. I always appreciate your post. That is very interesting I love reading and I am always searching for informative information like this. Well written article
Machine Learning With TensorFlow Training and Course in Muscat
| CPHQ Online Training in Singapore. Get Certified Online

kate technologies said...

Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
app development companies in karimnagar
android app development company in nalgonda

vimal kumar said...

Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.

Devops Training in bangalore
Digital Marketing Training in bangalore
Data Science Training in bangalore
Java Training in bangalore

Shanthi Cabs said...

Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I have bookmarked more article from this website. Such a nice blog you are providing ! Kindly Visit Us @ Best Travels in Madurai | Tours and Travels in Madurai | Madurai Travels

jaya devan said...

You are doing a great job. I would like to appreciate your work for good accuracy
PHP Training in Chennai | PHP Course in Chennai

Anjali Siva said...

Fabulous post admin, it was too good and helpful. Waiting for more updates.
DevOps certification in Chennai
DevOps Training in Chennai
AWS Training in Chennai
AWS course in Chennai
Data Science Course in Chennai
Data Science Training in Chennai
DevOps Training in Velachery
DevOps Training in Tambaram