Send HTML Email in PHP via SMTP

On 1/08/2016

Hi! Happy New Year 2016:)

This is my first post in 2016 for Koding Made Simple. Today we’ll see how to send html email in php. PHP’s mail() function is simple and effective and let you send emails with text/html contents and with attachments. Though it has some shortcomings compared to other mailer libraries like PHPMailer, it does the job and comes in-built with PHP package. Plain text emails are good enough, but the ability to send html emails is more powerful in email marketing and other promotions.

HTML emails include html tags and let you add images, format texts and other eye-catching call-to-action buttons etc. But you have to add additional headers for mailing them.


Using SMTP for Sending Email

You can send mails via third party Mail Servers but you need authentication first. That is if you want to send email via Gmail in php, then you must have a working gmail account and provide the accounts email-id and password for authentication. We’ll see how to set up the php configuration for sending email via gmail smtp server.

How to Send HTML Email in PHP Using Gmail SMTP

You need to change the settings in two places i.e., php.ini and sendmail.ini files. You must remove the ; at the starting of the line to enable the settings in these files. Please note the configuration is given for the xampp package. Open ‘php.ini’ file located at C:\xampp\php\php.ini and edit the settings like below.


[mail function]
smtp_port = 587
sendmail_from =
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
Next open the ‘sendmail.ini’ file located at C:\xampp\sendmail\sendmail.ini and make the below changes.


smtp_server =
smtp_port = 587
auth_username =
auth_password = christmascontest
force_sender =

Save the files and restart apache server for the changes to reflect. Now you are ready to send send html email using php mail() function.

Create a php file and write down the below php mailer code to it and save.

PHP Code for Sending HTML Email

// from email
$from = ''; // change this
// to email
$to = ',,'; // change this
// subject
$subject = 'Christmas Contest Announcement';

// html message
$htmlmsg = '<html>
        <title>Christmas Contest Winners</title>
        <h1>Hi! We are glad to announce the Christmas contest winners...</h1>
            <tr style="background-color: #EEE;">
                <th width="25%">#</th>
                <th width="35%">Ticket No.</th>
            <tr style="background-color: #EEE;">

// set content type header for html email
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

// set additional headers
$headers .= 'From: Christmas Contest <>' . "\r\n";
$headers .= 'Cc:' . "\r\n";

// send email
if (mail($to, $subject, $htmlmsg, $headers))
    echo "Email sent successfully!";
    echo "Error sending email! Please try again later...";

Change $from to your gmail-id and $to variable to the recipients email-id.

Plus the code includes content type header which is a must for sending html emails and additional headers should be appended with CRLF.

Note: If you have enabled two-way authentication for Google account then please disable it for this code to work.

That’s all about sending html email in php via smtp server.

Read Also:


Contact Form


Email *

Message *