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
+14.5k Golang : Send email with attachment(RFC2822) using Gmail API example
+5.7k Unix/Linux/MacOSx : Get local IP address
+7.2k Golang : File system scanning
+14.7k Golang : Get URI segments by number and assign as variable example
+18.5k Golang : Generate thumbnails from images
+8.3k Useful methods to access blocked websites
+14.9k Golang : Search folders for file recursively with wildcard support
+6.7k Golang : Output or print out JSON stream/encoded data
+19.8k Golang : Count JSON objects and convert to slice/array
+19.9k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader