How to Send Nice HTML Email with PHP
If you are making an email software then you must know how to make email templates and send with proper headers in stylish format not a plain email with only text. In PHP with mail() function you can send a simple email but but there is some headers which make it more flexible you can attached files format it in HTML and many more. I am going to explain you how to make an HTML email and give you a demo and script to download.
<?php mail($to, $subject, $message, $headers); ?>
Send HTML email with PHP needs same method as normal email.
<?php $to = "[email protected]"; $from = '[email protected]'; $headers = "From: " . strip_tags($from) . "\r\n"; $headers .= "Reply-To: ". strip_tags($from) . "\r\n"; $headers .= "CC: [email protected]\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; ?>
To send HTML email you have to add headers which is optional by default where we pass Content-Type type declaration telling mail services that parse this email as HTML.
The headers give us lots of important functions to our emails like file attachment, content type etc.
Use HTML tags for design
<?php $message = '<html><body>'; $message .= '<table width="100%"; rules="all" style="border:1px solid #3A5896;" cellpadding="10">'; $message .= "<tr><td><img src='https://www.phpgang.com/wp-content/uploads/gang.jpg' alt='PHP Gang' /></td></tr>"; $message .= "<tr><td colspan=2>Dear \$Name,<br /><br />We thank you for subscribe phpgang.com you are now in phpgang download list you can download any source package from our site.</td></tr>"; $message .= "<tr><td colspan=2 font='colr:#999999;'><I>PHPGang.com<br>Solve your problem. :)</I></td></tr>"; $message .= "</table>"; $message .= "</body></html>"; ?>
Now your emails look nicer this email is directly coming to you in formatted style to be easy to eyes.
Is this a serious article? This is so so bad…
@e6bdecd96835a47645760f21d22281a4:disqus what is bad in it?
Everything is bad…. tell me what is g00d? @huzoorbux:disqus
ok @92ffeca1ad5be857bbbc4878f572ff21:disqus i will make good articles next time.
demo is working but when we download source code… then all are fake… don’t use it… fake demo
@92ffeca1ad5be857bbbc4878f572ff21:disqus what is wrong in source code it is the same code which is in demo site.
This is awesome 🙂
sir i didnt receive in my email the sample in your demo .. why is it?
use mail() function, it will work
I tried to send myself an email using your demo but it doesn’t work.
Demo will not send email download script and test it on your website.
thank u bhai
Are koi yeh sab tho chalta rahta hai………..
With this code i can not send email in msg its show me blank mail