wp_mail() – Sends an email, similar to PHP’s mail function.

You appear to be a bot. Output may be restricted

Description

Sends an email, similar to PHP's mail function.

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.

Usage

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

Parameters

$to
( string|string[] ) required – Array or comma-separated list of email addresses to send message.
$subject
( string ) required – Email subject.
$message
( string ) required – Message contents.
$headers
( string|string[] ) optional – Optional. Additional headers.
$attachments
( string|string[] ) optional – Optional. Paths to files to attach.

Returns

bool Whether the email was sent successfully.

Source

File name: wordpress/wp-includes/pluggable.php


Lines:

1 to 100 of 424
  function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
    // Compact the input, apply the filters, and extract them back out.

    
/**
 * Filters the wp_mail() arguments.
 *
 * @since 2.2.0
 *
 * @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.
 * }
 */
    $atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) );

    
/**
 * 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
 *
 * @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.
 * }
 */
    $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 ) );
    }
    global $phpmailer;

    // (Re)create it, if it's gone missing.
    if ( ! ( $phpmailer instanceof PHPMailer\PHPMailer\PHPMailer ) ) {
      require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
      require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php';
      require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';
      $phpmailer = new PHPMailer\PHPMailer\PHPMailer( true );

      $phpmailer::$validator = static function ( $email ) {
        return (bool) is_email( $email );
      };
    }

    // Headers.
    $cc       = array();
    $bcc      = array();
    $reply_to = array();

    if ( empty( $headers ) ) {
      $headers = array();
    } else {
      if ( ! is_array( $headers ) ) {
        /*
				 * Explode the headers out, so this function can take
				 * both string headers and an array of headers.

 View on GitHub View on Trac