Using WordPress ‘wp_mail’ PHP action

The wp_mail() WordPress PHP function is used to send an email, similar to PHP’s mail function.

Usage

wp_mail($to, $subject, $message, $headers, $attachments);

Parameters

  • $to (string|string[]): Required. Array or comma-separated list of email addresses to send the message to.
  • $subject (string): Required. Email subject.
  • $message (string): Required. Message contents.
  • $headers (string|string[]): Optional. Additional headers. Default: ”.
  • $attachments (string|string[]): Optional. Paths to files to attach. Default: array().

Return

bool: Whether the email was sent successfully.

More Information

See WordPress Developer Resources: wp_mail()

Examples

Basic usage

Send a simple email.

$to = '[email protected]';
$subject = 'Hello!';
$body = 'This is a simple email.';
wp_mail($to, $subject, $body);

Send an email with a header

Send an email with a “From” header.

$to = '[email protected]';
$subject = 'Hello!';
$body = 'This is an email with a custom "From" header.';
$headers = 'From: My Name <[email protected]>';
wp_mail($to, $subject, $body, $headers);

Send an email with multiple recipients

Send an email to multiple recipients.

$to = array('[email protected]', '[email protected]');
$subject = 'Hello, recipients!';
$body = 'This email was sent to multiple recipients.';
wp_mail($to, $subject, $body);

Send an email with an attachment

Send an email with a file attachment.

$to = '[email protected]';
$subject = 'Hello!';
$body = 'This email has an attachment.';
$attachments = array('/path/to/file1.png', '/path/to/file2.pdf');
wp_mail($to, $subject, $body, '', $attachments);

Send an HTML email

Send an email with HTML content by setting the content type.

$to = '[email protected]';
$subject = 'Hello!';
$body = '<h1>Hello!</h1><p>This is an <strong>HTML</strong> email.</p>';
$headers = 'Content-Type: text/html; charset=UTF-8';
wp_mail($to, $subject, $body, $headers);