$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.244

Latest news

  • ProcessWire Weekly #559
    The 559th issue of ProcessWire Weekly brings in all the latest news from the ProcessWire community. Modules, sites, and more. Read on!
    Weekly.pw / 25 January 2025
  • ProcessWire 3.0.244 new main/master version
    ProcessWire 3.0.244 is our newest main/master/stable version. It's been more than a year in the making and is packed with tons of new features, issue fixes, optimizations and more. This post covers all the details.
    Blog / 18 January 2025
  • Subscribe to weekly ProcessWire news

“To Drupal, or to ProcessWire? The million dollar choice. We decided to make an early switch to PW. And in retrospect, ProcessWire was probably the best decision we made. Thanks are due to ProcessWire and the amazing system and set of modules that are in place.” —Unni Krishnan, Founder of PigtailPundits