diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index 3f9304c711..6ecd672706 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -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; } diff --git a/tests/phpunit/tests/pluggable/wpMail.php b/tests/phpunit/tests/pluggable/wpMail.php index 68982d6026..c592555b40 100644 --- a/tests/phpunit/tests/pluggable/wpMail.php +++ b/tests/phpunit/tests/pluggable/wpMail.php @@ -454,6 +454,50 @@ class Tests_Pluggable_wpMail extends WP_UnitTestCase { $this->assertSame( $expected_error_data, $call_args[0]->get_error_data() ); } + /** + * Test that attachment file names are derived from array values when their + * associative array keys are numeric. + * + * @ticket 28407 + */ + public function test_wp_mail_sends_attachments_with_original_name() { + wp_mail( 'user@example.org', 'Subject', 'Hello World', '', array( + DIR_TESTDATA . '/images/canola.jpg', + DIR_TESTDATA . '/images/waffles.jpg' + ) ); + + /** @var PHPMailer $mailer */ + $mailer = tests_retrieve_phpmailer_instance(); + + $attachments = $mailer->getAttachments(); + + $this->assertTrue( $mailer->attachmentExists() ); + $this->assertSame( $attachments[0][1], $attachments[0][2] ); + $this->assertSame( $attachments[1][1], $attachments[1][2] ); + } + + /** + * Test that attachment file names are derived from array keys when they + * are non-empty strings. + * + * @ticket 28407 + */ + public function test_wp_mail_sends_attachments_with_custom_name() { + wp_mail( 'user@example.org', 'Subject', 'Hello World', '', array( + 'alonac.jpg' => DIR_TESTDATA . '/images/canola.jpg', + 'selffaw.jpg' => DIR_TESTDATA . '/images/waffles.jpg' + ) ); + + /** @var PHPMailer $mailer */ + $mailer = tests_retrieve_phpmailer_instance(); + + $attachments = $mailer->getAttachments(); + + $this->assertTrue( $mailer->attachmentExists() ); + $this->assertSame( 'alonac.jpg', $attachments[0][2] ); + $this->assertSame( 'selffaw.jpg', $attachments[1][2] ); + } + /** * @ticket 50720 */