mirror of
git://develop.git.wordpress.org/
synced 2025-02-06 23:50:43 +01:00
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:
parent
d8f311800d
commit
45053ef512
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user