Send HTML Email in PHP via SMTP

On 4/06/2018

Hi! Welcome to 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:


  1. Nice article.. thank you for sharing it.

  2. Hello and thanks for this tutorial, i will ask, what if I don't want to use localhost, but a hosting site to send html email with phpmailer, how do I go about it? Thanks

    1. Then you get the smtp settings from your hosting provider and use it. Also make sure sending email is possible with your account.


    2. Thanks for your reply. Actually, i have done that, thing is, anytime i use the code below with Html as its body, messages are dropped into spam, but with plain text only, my messages are dropped into inbox.
      So, i'm hoping you can help me out. Thanks









      $mail->Subject='PHPMailer Test';
      $mail->Body='PHPMailer Test Subject via Sendmail, basic';

      echo'Mail sent';
      echo "Mail sending failed";


Contact Form


Email *

Message *