Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

mail

Sends an email through the server's mail program.

Syntax

mail(string $to, string $subject, string $message, array|string $additional_headers = []): bool

Parameters

ParameterTypeDescription
$tostringThe recipient's address.
$subjectstringThe subject.
$messagestringThe body.
$additional_headersarray|stringOptional. Headers: `From`, `Reply-To`, `Content-Type`.

Return value

bool`true` if the email was accepted for delivery.

Examples

A simple email:

<?php
 
// send.php
 
$sent = mail('anna@example.com', 'Order received', 'Thank you for your order #42.', [
    'From' => 'shop@example.com',
    'Content-Type' => 'text/plain; charset=utf-8',
]);
 
echo $sent ? 'Sent' : 'Failed';

Notes

true does not mean the email arrived. Real projects send email over SMTP with PHPMailer or Symfony Mailer.

See also

  • filter_var — Validates or sanitises a value by a given rule.
  • error_log — Writes a message to the error log.
  • header — Sends an HTTP response header.

Related recipes

In the tutorial

  • Sending email — How to send email from PHP — the mail function, the PHPMailer library and SMTP, HTML emails, attachments and protecting headers.

Thanks for reading!

Donate to the site — we work hard for you.