$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 #549
    In the 549th issue of ProcessWire Weekly we’re going to check out the latest core updates, highlight one older yet still very relevant third party module, and more. Read on!
    Weekly.pw / 17 November 2024
  • Custom Fields Module
    This week we look at a new ProFields module named Custom Fields. This module provides a way to rapidly build out ProcessWire fields that contain any number of subfields/properties within them.
    Blog / 30 August 2024
  • 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