wppaste
WordPress

wp_mail( string|string[] $to, string $subject, string $message, string|string[] $headers = '', string|string[] $attachments = array(), string|string[] $embeds = array() ): bool

Since
1.2.1, 5.5.0, 6.9.0, 6.9.0
Source
wp-includes/pluggable.php:189
Sends an email, similar to PHP's mail function.

Description

A true return value does not automatically mean that the user received the email successfully. It just only means that the method used was able to process the request without any errors.

The default content type is text/plain which does not allow using HTML.
However, you can set the content type of the email by using the 'wp_mail_content_type' filter.

The default charset is based on the charset used on the blog. The charset can be set using the 'wp_mail_charset' filter.

When using the $embeds parameter to embed images for use in HTML emails, reference the embedded file in your HTML with a cid: URL whose value matches the file's Content-ID. By default, the Content-ID (cid) used for each embedded file is the key in the embeds array, unless modified via the 'wp_mail_embed_args' filter. For example:

<img src="cid:0" alt="Logo"> <img src="cid:my-image" alt="Image">

You may also customize the Content-ID for each file by using the 'wp_mail_embed_args' filter and setting the cid value.

Compatibility

WordPress
since 6.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$tostring|string[]
Array or comma-separated list of email addresses to send message.
$subjectstring
Email subject.
$messagestring
Message contents.
$headersstring|string[]optional
Additional headers.Default: ''
$attachmentsstring|string[]optional
Paths to files to attach.Default: array()
$embedsstring|string[]optional
Paths to files to embed.Default: array()

Return value

bool
Whether the email was sent successfully.

Performance profile

How much work a call to wp_mail() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Expensive

Sends mail.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
6

Executed per call on PHP 8.5. The body compiles to 610.

Plugin surface
10 hooks

Third-party callbacks on 'wp_mail', 'pre_wp_mail', 'wp_mail_from' run inside this call, and their cost is not bounded by anything here.

Called by
28

28 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • mailsends mailwp_mail()this function does it
  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()one call below wp_mail()

Further down the call graph this can also reach cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_mail() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always6is_email()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev610662
8.5610662
8.461066277 fewer instructions than PHP 8.3
8.3687662
8.26876622 more instructions than PHP 8.1
8.16856621 more instruction than PHP 7.4
7.4684662

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 11

11 hooks fire while wp_mail() runs, in this order:

  1. apply_filters( wp_mail )filterline 209 (+20 into the body)

    Filters the wp_mail() arguments.

  2. apply_filters( pre_wp_mail )filterline 233 (+44 into the body)

    Filters whether to preempt sending an email.

  3. apply_filters( wp_mail_from )filterline 437 (+248 into the body)

    Filters the email address to send from.

  4. apply_filters( wp_mail_from_name )filterline 446 (+257 into the body)

    Filters the name to associate with the "from" email address.

  5. do_action( wp_mail_failed )actionline 455 (+266 into the body)

    Fires after a PHPMailer exception is caught.

  6. apply_filters( wp_mail_content_type )filterline 521 (+332 into the body)

    Filters the wp_mail() content type.

  7. apply_filters( wp_mail_charset )filterline 542 (+353 into the body)

    Filters the default wp_mail() charset.

  8. apply_filters( wp_mail_embed_args )filterline 588 (+399 into the body)

    Filters the arguments for PHPMailer's addEmbeddedImage() method.

  9. do_action( phpmailer_init )actionline 622 (+433 into the body)

    Fires after PHPMailer is initialized.

  10. do_action( wp_mail_succeeded )actionline 651 (+462 into the body)

    Fires after PHPMailer has successfully sent an email.

  11. do_action( wp_mail_failed )actionline 665 (+476 into the body)

    Fires after a PHPMailer exception is caught.

Uses · 12

Used by · 28

Show all 28

Source code

	function wp_mail( $to, $subject, $message, $headers = '', $attachments = array(), $embeds = array() ) {		// Compact the input, apply the filters, and extract them back out. 		/**		 * Filters the wp_mail() arguments.		 *		 * @since 2.2.0		 * @since 6.9.0 The `$embeds` element was added to the `$args` array.		 *		 * @param array $args {		 *     Array of the `wp_mail()` arguments.		 *		 *     @type string|string[] $to          Array or comma-separated list of email addresses to send message.		 *     @type string          $subject     Email subject.		 *     @type string          $message     Message contents.		 *     @type string|string[] $headers     Additional headers.		 *     @type string|string[] $attachments Paths to files to attach.		 *     @type string|string[] $embeds      Paths to files to embed.		 * }		 */		$atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments', 'embeds' ) ); 		/**		 * Filters whether to preempt sending an email.		 *		 * Returning a non-null value will short-circuit {@see wp_mail()}, returning		 * that value instead. A boolean return value should be used to indicate whether		 * the email was successfully sent.		 *		 * @since 5.7.0		 * @since 6.9.0 The `$embeds` element was added to the `$atts` array.		 *		 * @param null|bool $return Short-circuit return value.		 * @param array     $atts {		 *     Array of the `wp_mail()` arguments.		 *		 *     @type string|string[] $to          Array or comma-separated list of email addresses to send message.		 *     @type string          $subject     Email subject.		 *     @type string          $message     Message contents.		 *     @type string|string[] $headers     Additional headers.		 *     @type string|string[] $attachments Paths to files to attach.		 *     @type string|string[] $embeds      Paths to files to embed.		 * }		 */		$pre_wp_mail = apply_filters( 'pre_wp_mail', null, $atts ); 		if ( null !== $pre_wp_mail ) {			return $pre_wp_mail;		} 		if ( isset( $atts['to'] ) ) {			$to = $atts['to'];		} 		if ( ! is_array( $to ) ) {			$to = explode( ',', $to );		} 		if ( isset( $atts['subject'] ) ) {			$subject = $atts['subject'];		} 		if ( isset( $atts['message'] ) ) {			$message = $atts['message'];		} 		if ( isset( $atts['headers'] ) ) {			$headers = $atts['headers'];		} 		if ( isset( $atts['attachments'] ) ) {			$attachments = $atts['attachments'];		} 		if ( ! is_array( $attachments ) ) {			$attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );		} 		if ( isset( $atts['embeds'] ) ) {			$embeds = $atts['embeds'];

Changelog

Introduced in 1.2.1. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.9.7
Parameter $embeds added.verified against source
6.9.0
Improved Content-Type header handling for multipart messages.from the docblock
6.9.0
The $embeds parameter was added.from the docblock
5.5.0
is_email() is used for email validation, instead of PHPMailer's default validator.from the docblock
1.2.1
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/pluggable.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.