January 22, 2024 5:02 am

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.

how-to-send-nice-html-email-wit- php

[wpdm_file id=34]
     mail($to, $subject, $message, $headers);

Send HTML email with PHP needs same method as normal email.

  $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

$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.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.

Tutorial Categories:

14 responses to “How to Send Nice HTML Email with PHP”

  1. Lawl says:

    Is this a serious article? This is so so bad…

  2. Krunal Bhingradiya says:

    demo is working but when we download source code… then all are fake… don’t use it… fake demo

  3. 77-thoughts.com says:

    This is awesome 🙂

  4. jhay says:

    sir i didnt receive in my email the sample in your demo .. why is it?

  5. Anuj Gupta says:

    Not working.
    I tried to send myself an email using your demo but it doesn’t work.

  6. Satyendra Yadav says:

    thank u bhai

  7. Prashant says:

    With this code i can not send email in msg its show me blank mail

Leave a Reply

Your email address will not be published. Required fields are marked *