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 = '[email protected]';
$from = '[email protected]';
$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
+27.5k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+28.9k Golang : Saving(serializing) and reading file with GOB
+38.8k Golang : How to iterate over a []string(array)
+9.9k Golang : Bcrypting password
+6.3k Golang : Calculate diameter, circumference, area, sphere surface and volume
+8.9k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+5.5k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+22.8k Golang : Randomly pick an item from a slice/array example
+6.2k PHP : Proper way to get UTF-8 character or string length
+10.7k Golang : Fix go.exe is not compatible with the version of Windows you're running
+13.7k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+17.5k Golang : How to make a file read only and set it to writable again?