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

41 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

Unknown said...

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

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

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!

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

Anonymous said...

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

Unknown said...

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

Unknown said...

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

Unknown said...

Hi,

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

Ramya Krishnan said...

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

mani said...

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

Unknown 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

Peter Floyd 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

Unknown said...

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

Ancy merina said...
This comment has been removed by the author.
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

Unknown 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

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..
DOT NET training in btm

dot net training institute in btm

dot net course in btm

best dot net training institute in btm

rama 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

Unknown 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

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

liza said...


NICE for giving a chance to share ideas for your comuty i really tha Deer Hunting Tips Camping Trips Guide DEER HUNTING TIPS travel touring tips


nks for that great post.

liza said...


Thank you for such a sweet tutorial - all this time later, I've found it and love the end result. I appreciate the time you spent sharing your skills.
Still Hunting Method
Hunting psych tips Survival Tips Travel Touring Tips

liza said...


NICE for giving a chance to share ideas for your community I really thanks for that great post.travel trekking tips
see the link Tent Camping 101 Exploring Smithriver

manisha said...

It is very useful for me to learn and understand easily.
PHP Training in Gurgaon
PHP Course in Gurgaon

nash b said...

Nice..
bitwise aptitude questions

how to hack flipkart legally

zenq interview questions

count ways to n'th stair(order does not matter)

zeus learning subjective test

ajax success redirect to another page with data

l&t type 2 coordination chart

html rollover image

hack android phone using cmd

how to hack internet speed upto 100mbps

nash b said...

Good..
internships in chennai

winter internship mechanical engineering

internship for aeronautical engineering students in india 2019

kaashiv

list of architectural firms in chennai for internship

paid internships in pune for computer science students

diploma final year project topics for information technology

internship

data science internship report

inplant training

shalini said...

good....nice
category/advocate-resume
category/agriculture-forestry-fishing
category/android-developer-resume
category/assistant-professor-resume
category/chartered-accountant-resume
category/database-resume
category/design-engineer-resume
category/developer-resume
category/engineer-resume
category/entrepreneur-and-financial-services-resume

svrtechnologies said...

This post is really nice and informative. The explanation given is really comprehensive and useful...

sap fico online training
sap fico training videos

devi said...

It is truly an honour to run across informational content like the one you have written. You are evidently knowledgeable on this written topic and you have unique views to share. Data Science Training In Chennai | Certification | Data Science Courses in Chennai | Data Science Training In Bangalore | Certification | Data Science Courses in Bangalore | Data Science Training In Hyderabad | Certification | Data Science Courses in hyderabad | Data Science Training In Coimbatore | Certification | Data Science Courses in Coimbatore | Data Science Training | Certification | Data Science Online Training Course

Jayalakshmi said...

It's very useful blog post with inforamtive and insightful content and i had good experience with this information about PHP send HTML email, The complete php mail sending script.
hardware and networking training in chennai

hardware and networking training in tambaram

xamarin training in chennai

xamarin training in tambaram

ios training in chennai

ios training in tambaram

iot training in chennai

iot training in tambaram

praveen said...

First i got a great blog .I will be interested in more similar topics. i see you got really very useful topics,
i will be always checking your blog thanks
web designing training in chennai

web designing training in porur

digital marketing training in chennai

digital marketing training in porur

rpa training in chennai

rpa training in porur

tally training in chennai

tally training in porur

deiva said...

Informative blog and it was up to the point describing the information very effectively. Thanks to blog author for wonderful and informative post...
java training in chennai

java training in omr

aws training in chennai

aws training in omr

python training in chennai

python training in omr

selenium training in chennai

selenium training in omr

Ramesh Sampangi said...

Really awesome blog. Informative content and useful to my people. Thanks for sharing this blog with us.
Data Science Course Training in Hyderabad

360DigiTMG said...

I’m happy I located this blog! From time to time, students want to recognize the keys of productive literary essays. Your first-class knowledge about this good post can become a proper basis for such people. nice one
business analytics course in hyderabad