Skip to content

Inline image attachments with wp_mail() #9038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 63 additions & 2 deletions src/wp-includes/pluggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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.

/**
Expand All @@ -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.
Expand All @@ -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 );
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 );
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/tests/pluggable/signatures.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public function get_pluggable_function_signatures() {
'message',
'headers' => '',
'attachments' => array(),
'embeds' => array(),
),
'wp_authenticate' => array( 'username', 'password' ),
'wp_logout' => array(),
Expand Down
Loading