$mail->mail() method

Send an email, drop-in replacement for PHP mail() that uses the same arguments

This is an alternative to using the $mail->send() method, and may be simpler for those converting existing PHP mail() calls to WireMail calls.

This function duplicates the same arguments as PHP’s mail function, enabling you to replace an existing PHP mail(…) call with $mail->mail(…).

But unlike PHP’s mail function, this one can also send HTML (or multipart) emails if you provide an $options array for the $message argument (rather than a string). See the options array for the $mail->send() method for details.

Example

// 1. Basic PHP mail() style usage
$mail->mail('ryan@processwire.com', 'Subject', 'Message body');

// 2. PHP mail() style usage with with $headers argument
$mail->mail('ryan@processwire.com', 'Subject', 'Message body', 'From: hello@world.com');

// 3. Alternate usage with html and text body
$mail->mail('ryan@processwire.com', 'Subject', [
  'bodyHTML' => '<html><body><h1>Message HTML body</h1></body</html>',
  'body' => 'Message text body',
  'from' => 'hello@world.com',
]);

Usage

// basic usage
$bool = $mail->mail($to, string $subject, $message);

// usage with all arguments
$bool = $mail->mail($to, string $subject, $message, array $headers = []);

Arguments

NameType(s)Description
tostring, array

Email address TO. For multiple, specify CSV string or array.

subjectstring

Email subject

messagestring, array

Email body (PHP mail style), OR specify $options array with any of the following:

  • bodyHTML (string): Email body (HTML)
  • body (string): Email body (text). If not specified, and bodyHTML is, then text body will be auto-generated.
  • from (string): From email address
  • replyTo (string): Reply-to email address
  • headers (array): Associative array of header name => header value
headers (optional)array

Optional additional headers as [name=value] array or "Name: Value" newline-separated string. Use this argument to duplicate PHP mail() style arguments. No need to use if you used $options array for the $message argument.

Return value

bool

True on success, false on fail.


$mail methods and properties

API reference based on ProcessWire core version 3.0.236

Latest news

  • ProcessWire Weekly #520
    In the 520th issue of ProcessWire Weekly we'll check out some of the latest additions to the ProcessWire module's directory, share some highlights from the latest weekly update from Ryan, and more. Read on!
    Weekly.pw / 27 April 2024
  • ProFields Table Field with Actions support
    This week we have some updates for the ProFields table field (FieldtypeTable). These updates are primarily focused on adding new tools for the editor to facilitate input and management of content in a table field.
    Blog / 12 April 2024
  • Subscribe to weekly ProcessWire news

“…building with ProcessWire was a breeze, I really love all the flexibility the system provides. I can’t imagine using any other CMS in the future.” —Thomas Aull