Skip to content
PHP Logo

What are common pitfalls when using the mail() function in PHP?

One common pitfall when using the mail() function in PHP is not properly setting the headers, which can result in emails being marked as spam or not being delivered at all. To solve this issue, make sure to include necessary headers such as From, Reply-To, and Content-Type in the mail() function.

php
$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";
$headers = "From: sender@example.com\r\n";
$headers .= "Reply-To: sender@example.com\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

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