$to, 'subject' => $subject, 'message' => $message, 'headers' => $headers, 'attachments' => $attachments, ) ); // Inline images such as signatures travel as embedded attachments // (cid:) because most mail apps refuse data: image sources (2.3.3). $embedded = self::extractInlineImages( (string) $mail_args['message'] ); $mail_args['message'] = $embedded['html']; $embed = null; if ( $embedded['images'] ) { $images = $embedded['images']; $embed = static function ( $phpmailer ) use ( $images ) { foreach ( $images as $cid => $image ) { $phpmailer->addStringEmbeddedImage( $image['data'], $cid, $image['name'], 'base64', $image['mime'] ); } }; add_action( 'phpmailer_init', $embed ); } $sent = wp_mail( $mail_args['to'], $mail_args['subject'], $mail_args['message'], $mail_args['headers'], $mail_args['attachments'] ); if ( $embed ) { remove_action( 'phpmailer_init', $embed ); } // Allow filter on send result return (bool) apply_filters( 'subtleforms/mail/sent', $sent, $mail_args ); } /** * Swap data: PNG/JPEG image sources in an HTML body for cid: references. * * @param string $html Email body. * @return array{html:string,images:array} */ public static function extractInlineImages( string $html ): array { $images = array(); if ( false === strpos( $html, 'data:image/' ) ) { return array( 'html' => $html, 'images' => $images ); } $html = (string) preg_replace_callback( '#src=(["\'])data:image/(png|jpeg);base64,([A-Za-z0-9+/=]+)\1#', static function ( $m ) use ( &$images ) { $data = base64_decode( $m[3], true ); if ( false === $data || '' === $data ) { return $m[0]; } $cid = 'sf-inline-' . ( count( $images ) + 1 ) . '-' . substr( md5( $m[3] ), 0, 8 ); $images[ $cid ] = array( 'data' => $data, 'mime' => 'image/' . $m[2], 'name' => $cid . ( 'png' === $m[2] ? '.png' : '.jpg' ), ); return 'src=' . $m[1] . 'cid:' . $cid . $m[1]; }, $html ); return array( 'html' => $html, 'images' => $images ); } }