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.,
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 = smtp.gmail.com smtp_port = 587 sendmail_from = email@example.com sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"Next open the ‘sendmail.ini’ file located at
C:\xampp\sendmail\sendmail.iniand make the below changes.
smtp_server = smtp.gmail.com smtp_port = 587 auth_username = firstname.lastname@example.org auth_password = christmascontest force_sender = email@example.com
Save the files and restart apache server for the changes to reflect. Now you are ready to send send html email using php
Create a php file and write down the below php mailer code to it and save.
PHP Code for Sending HTML Email
<?php // from email $from = 'firstname.lastname@example.org'; // change this // to email $to = 'email@example.com, firstname.lastname@example.org, email@example.com'; // change this // subject $subject = 'Christmas Contest Announcement'; // html message $htmlmsg = '<html> <head> <title>Christmas Contest Winners</title> </head> <body> <h1>Hi! We are glad to announce the Christmas contest winners...</h1> <table> <tr style="background-color: #EEE;"> <th width="25%">#</th> <th width="35%">Ticket No.</th> <th>Name</th> </tr> <tr> <td>#1</td> <td>P646MLDO808K</td> <td>Sally</td> </tr> <tr style="background-color: #EEE;"> <td>#2</td> <td>DFJ859LV9D5U</td> <td>Parker</td> </tr> <tr> <td>#3</td> <td>AU30HI8IHL96</td> <td>Justin</td> </tr> </table> </body> </html>'; // 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 <firstname.lastname@example.org>' . "\r\n"; $headers .= 'Cc: email@example.com' . "\r\n"; // send email if (mail($to, $subject, $htmlmsg, $headers)) echo "Email sent successfully!"; else echo "Error sending email! Please try again later..."; ?>
$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
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: