diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index 1dbac5e1d707c..ee845140cca0d 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -161,6 +161,7 @@ function cache_users( $user_ids ) { * @since 1.2.1 * @since 5.5.0 is_email() is used for email validation, * instead of PHPMailer's default validator. + * @since 6.9.0 Added $embeds parameter. * * @global PHPMailer\PHPMailer\PHPMailer $phpmailer * @@ -169,9 +170,10 @@ function cache_users( $user_ids ) { * @param string $message Message contents. * @param string|string[] $headers Optional. Additional headers. * @param string|string[] $attachments Optional. Paths to files to attach. + * @param string|string[] $embeds Optional. Paths to files to embed. * @return bool Whether the email was sent successfully. */ - function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) { + function wp_mail( $to, $subject, $message, $headers = '', $attachments = array(), $embeds = array() ) { // Compact the input, apply the filters, and extract them back out. /** @@ -187,9 +189,10 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() * @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' ) ); + $atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments', 'embeds' ) ); /** * Filters whether to preempt sending an email. @@ -209,6 +212,7 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() * @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 ); @@ -244,6 +248,15 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() if ( ! is_array( $attachments ) ) { $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) ); } + + if ( isset( $atts['embeds'] ) ) { + $embeds = $atts['embeds']; + } + + if ( ! is_array( $embeds ) ) { + $embeds = explode( "\n", str_replace( "\r\n", "\n", $embeds ) ); + } + global $phpmailer; // (Re)create it, if it's gone missing. @@ -531,6 +544,53 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() } } + if ( ! empty( $embeds ) ) { + foreach ( $embeds as $embed_path ) { + /** + * Filters the arguments for PHPMailer's addEmbeddedImage() method. + * + * @since 6.9.0 + * + * @param array $args { + * An array of arguments for `addEmbeddedImage()`. Using PHPMailer default values. + * + * @type string $path The path to the file. + * @type string $cid The Content-ID of the image. + * @type string $name The filename of the image. + * @type string $encoding The encoding of the image. Default 'base64'. + * @type string $type The MIME type of the image. Default is empty string, which lets PHPMailer auto-detect. + * @type string $disposition The disposition of the image. Default 'inline'. + * } + * @param string $embed_path The path to the file being embedded. + */ + $embed_args = apply_filters( + 'wp_mail_embed_args', + array( + 'path' => $embed_path, + 'cid' => md5( $embed_path ), + 'name' => basename( $embed_path ), + 'encoding' => 'base64', + 'type' => '', + 'disposition' => 'inline', + ), + $embed_path + ); + + try { + $phpmailer->addEmbeddedImage( + $embed_args['path'], + $embed_args['cid'], + $embed_args['name'], + $embed_args['encoding'], + $embed_args['type'], + $embed_args['disposition'] + ); + } catch ( PHPMailer\PHPMailer\Exception $e ) { + continue; + } + } + } + /** * Fires after PHPMailer is initialized. * @@ -563,6 +623,7 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() * @type string $message Message contents. * @type string[] $headers Additional headers. * @type string[] $attachments Paths to files to attach. + * @type string[] $embeds Paths to files to embed. * } */ do_action( 'wp_mail_succeeded', $mail_data ); diff --git a/tests/phpunit/tests/pluggable/signatures.php b/tests/phpunit/tests/pluggable/signatures.php index 3919e31232979..68b511b9d0d2d 100644 --- a/tests/phpunit/tests/pluggable/signatures.php +++ b/tests/phpunit/tests/pluggable/signatures.php @@ -137,6 +137,7 @@ public function get_pluggable_function_signatures() { 'message', 'headers' => '', 'attachments' => array(), + 'embeds' => array(), ), 'wp_authenticate' => array( 'username', 'password' ), 'wp_logout' => array(),