Make WordPress Core

Changeset 55030


Ignore:
Timestamp:
01/05/2023 10:47:06 AM (2 years ago)
Author:
johnjamesjacoby
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/pluggable.php

    r54952 r55030  
    518518
    519519        if ( ! empty( $attachments ) ) {
    520             foreach ( $attachments as $attachment ) {
     520            foreach ( $attachments as $filename => $attachment ) {
     521                $filename = is_string( $filename ) ? $filename : '';
     522
    521523                try {
    522                     $phpmailer->addAttachment( $attachment );
     524                    $phpmailer->addAttachment( $attachment, $filename );
    523525                } catch ( PHPMailer\PHPMailer\Exception $e ) {
    524526                    continue;
  • trunk/tests/phpunit/tests/pluggable/wpMail.php

    r54872 r55030  
    456456
    457457    /**
     458     * Test that attachment file names are derived from array values when their
     459     * associative array keys are numeric.
     460     *
     461     * @ticket 28407
     462     */
     463    public function test_wp_mail_sends_attachments_with_original_name() {
     464        wp_mail( 'user@example.org', 'Subject', 'Hello World', '', array(
     465            DIR_TESTDATA . '/images/canola.jpg',
     466            DIR_TESTDATA . '/images/waffles.jpg'
     467        ) );
     468
     469        /** @var PHPMailer $mailer */
     470        $mailer = tests_retrieve_phpmailer_instance();
     471
     472        $attachments = $mailer->getAttachments();
     473
     474        $this->assertTrue( $mailer->attachmentExists() );
     475        $this->assertSame( $attachments[0][1], $attachments[0][2] );
     476        $this->assertSame( $attachments[1][1], $attachments[1][2] );
     477    }
     478
     479    /**
     480     * Test that attachment file names are derived from array keys when they
     481     * are non-empty strings.
     482     *
     483     * @ticket 28407
     484     */
     485    public function test_wp_mail_sends_attachments_with_custom_name() {
     486        wp_mail( 'user@example.org', 'Subject', 'Hello World', '', array(
     487            'alonac.jpg'  => DIR_TESTDATA . '/images/canola.jpg',
     488            'selffaw.jpg' => DIR_TESTDATA . '/images/waffles.jpg'
     489        ) );
     490
     491        /** @var PHPMailer $mailer */
     492        $mailer = tests_retrieve_phpmailer_instance();
     493
     494        $attachments = $mailer->getAttachments();
     495
     496        $this->assertTrue( $mailer->attachmentExists() );
     497        $this->assertSame( 'alonac.jpg', $attachments[0][2] );
     498        $this->assertSame( 'selffaw.jpg', $attachments[1][2] );
     499    }
     500
     501    /**
    458502     * @ticket 50720
    459503     */
Note: See TracChangeset for help on using the changeset viewer.