HOWTO: Send a mail using mail()

Using mail() to send a simple email:

<?php
// The message
$message = "Line 1\nLine 2\nLine 3";

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70);

// Send
mail(
'caffinated@example.com', 'My Subject', $message);
?>

Sending mail with extra headers:

<?php
$to      =
'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From:
webmaster@example.com' . "\r\n" .
   'Reply-To:
webmaster@example.com' . "\r\n" .
   'X-Mailer: PHP/' . phpversion();

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

  • 2025 Users Found This Useful
Was this answer helpful?