How to send HTML in email via PHP?

Spread the love

Sometimes, we want to send HTML in email via PHP.

In this article, we’ll look at how to send HTML in email via PHP.

How to send HTML in email via PHP?

To send HTML in email via PHP, we can set the Content-Type header of the email.

For instance, we write

<?php
$to = "bob@example.com";

$subject = "Website Change Request";

$headers = "From: " . strip_tags($_POST["req-email"]) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST["req-email"]) . "\r\n";
$headers .= "CC: susan@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

$message = "<p><strong>This is strong text</strong> while this is not.</p>";

mail($to, $subject, $message, $headers);

to add "Content-Type: text/html; charset=UTF-8\r\n" to the $headers of the email message.

Then we set $message to a string with HTML content.

Finally, we call mail to send the HTML email message by calling it with $to, $subject, $message and $headers.

Conclusion

To send HTML in email via PHP, we can set the Content-Type header of the email.

Leave a Reply

Your email address will not be published.