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.

git-svn-id: https://develop.svn.wordpress.org/trunk@55030 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John James Jacoby
2023-01-05 10:47:06 +00:00
parent d8f311800d
commit 45053ef512
2 changed files with 48 additions and 2 deletions

View File

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