<?php
//define the receiver of the email
$to = 'test@email.com';
//define the subject of the email
$subject = 'Test HTML email';
// Generate a random boundary string
$mime_boundary = '_x'.sha1(time()).'x';
// Using the heredoc syntax to declare the headers
$headers = <<<HEADERS
From: Test <test@example.com>
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="PHP-alt$mime_boundary"
HEADERS;
// Use our boundary string to create plain text and HTML versions
$message = <<<MESSAGE
--PHP-alt$mime_boundary
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
This Is a Plain Text Email
This message has no HTML. http://w3schools.com
--PHP-alt$mime_boundary
Content-type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: 7bit
<html>
<body>
<h1>This Is an HTML Email</h1>
<p>
This message is composed in <a href="http://w3schools.com">HTML</a>.
</p>
</body>
</html>
--PHP-alt$mime_boundary--
MESSAGE;
// Send the message
if(!mail($to, $subject, $message, $headers))
{
// If the mail function fails, return an error message
echo "Something went wrong!";
}
else
{
// Return a success message if nothing went wrong
echo "Message sent successfully. Check your email!";
}
?>