Send a plain email with mail()
Strip line breaks from anything that goes into a header, otherwise attackers can inject extra headers (header injection). On servers like WampServer, mail() usually needs an SMTP service to work.
PHP · Send a plain email with mail()Intermediate
function clean_header(string $v): string
{
return trim(preg_replace('/[\r\n]+/', ' ', $v));
}
$to = 'you@example.com';
$subject = clean_header('New message from ' . $_POST['name']);
$body = "Name: " . $_POST['name'] . "\nEmail: " . $_POST['email'] . "\n\n" . $_POST['message'];
$headers = [
'From' => 'website@example.com', // your own address, not the visitor's
'Reply-To' => clean_header($_POST['email']),
'Content-Type' => 'text/plain; charset=UTF-8',
];
mail($to, $subject, $body, $headers); // array headers need PHP 7.2 or neweremailmailsecurity