Mail: allow custom attachment filenames in wp_mail().

Previous to this change, attachment filenames in outgoing emails could only ever be derived from their paths (passed in as a numerically indexed array of `$attachments`).

This changeset adds support for passing an associative `$attachments` array, where the key strings will be used as filenames instead.

Includes 2 new unit tests to ensure both array formats continue to work as intended.

Props johnjamesjacoby, ritteshpatel, swissspidy, syntaxart.
Fixes #28407.
Built from https://develop.svn.wordpress.org/trunk@55030


git-svn-id: http://core.svn.wordpress.org/trunk@54563 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
johnjamesjacoby 2023-01-05 10:49:16 +00:00
parent 995964e3f6
commit 0f0646eda0
2 changed files with 5 additions and 3 deletions

View File

@ -517,9 +517,11 @@ if ( ! function_exists( 'wp_mail' ) ) :
} }
if ( ! empty( $attachments ) ) { if ( ! empty( $attachments ) ) {
foreach ( $attachments as $attachment ) { foreach ( $attachments as $filename => $attachment ) {
$filename = is_string( $filename ) ? $filename : '';
try { try {
$phpmailer->addAttachment( $attachment ); $phpmailer->addAttachment( $attachment, $filename );
} catch ( PHPMailer\PHPMailer\Exception $e ) { } catch ( PHPMailer\PHPMailer\Exception $e ) {
continue; continue;
} }

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.2-alpha-55029'; $wp_version = '6.2-alpha-55030';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.