PHP : Send HTML email
Sending email with PHP is easy and in this tutorial, we will learn how to send HTML based email with raw PHP. To get the code below to work, please change $to
and $from
to your own email addresses.
emailHTML.php :
<?php
$to = 'to@destination.com';
$from = 'from@source.com';
$subject = 'Send HTML email with PHP example';
$header = 'From: '.$from.PHP_EOL.
'Reply-to: '.$from.PHP_EOL.
'MIME-Version: 1.0'.PHP_EOL.
'Content-Type: text/html; charset=ISO-8859-1'.PHP_EOL.
'Content-Transfer-Encoding: 8bit'.PHP_EOL.
'X-Mailer: PHP/'.PHP_VERSION.PHP_EOL;
$message = '
<html>
<head>
</head>
<body>
<h4>Hi,</h4>
<p>
You have successfully sent HTML email via PHP!
</p>
</body>
</html>';
ini_set('sendmail_from', $from);
if (mail($to, $subject, $message, $header)) {
echo 'Sent';
} else {
echo 'Error sending....\n';
}
?>
Output after executing
> php emailHTML.php
if everything goes smoothly, then you should see
Sent
Check your email to see if you receive the HTML email correctly.
That's all. Hope you learn something useful with this short tutorial.
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+43.1k Golang : Get hardware information such as disk, memory and CPU usage
+45.9k Golang : Read tab delimited file with encoding/csv package
+5k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+18.3k Golang : Get download file size
+4.8k Google : Block or disable caching of your website content
+11k Golang : Fix - does not implement sort.Interface (missing Len method)
+16.8k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+5.7k Golang : Error handling methods
+11.2k Golang : Post data with url.Values{}
+29.2k Golang : Saving(serializing) and reading file with GOB
+4.6k Adding Skype actions such as call and chat into web page examples