Make WordPress Core


Ignore:
Timestamp:
09/01/2025 04:02:25 AM (13 months ago)
Author:
TimothyBlynJacobs
Message:

Mail: Support inline attachments.

MIME allows for referencing included attachments by their Content-ID header using the cid URL scheme. This can be used to embed images inline to the HTML message. For example, <img src="cid:logo">, will display the contents of message part with the Content-Id: <logo> header.

The wp_mail() function now supports including inline attachments through a new $embeds parameter. It accepts a map of Content-ID values to file paths. The wp_mail_embed_args filter can be used to customize the resulting PHPMailer::addEmbeddedImage method call.

Props jesin, swissspidy, chrisvendiadvertisingcom, SirLouen, mukesh27, yashjawale, iamadisingh.
Fixes #28059.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/pluggable/wpMail.php

    r58097 r60698  
    555555                $this->assertNotSame( 'user1', $phpmailer->AltBody );
    556556        }
     557
     558        /**
     559         * Test that wp_mail() can send embedded images.
     560         *
     561         * @ticket 28059
     562         * @covers ::wp_mail
     563         */
     564        public function test_wp_mail_can_send_embedded_images() {
     565                $embeds = array(
     566                        'canola' => DIR_TESTDATA . '/images/canola.jpg',
     567                        DIR_TESTDATA . '/images/test-image-2.gif',
     568                        DIR_TESTDATA . '/images/avif-lossy.avif',
     569                );
     570
     571                $message = '';
     572                foreach ( $embeds as $key => $path ) {
     573                        $message .= '<p><img src="cid:' . $key . '" alt="" /></p>';
     574                }
     575
     576                wp_mail(
     577                        'user@example.org',
     578                        'Embedded images test',
     579                        $message,
     580                        'Content-Type: text/html',
     581                        array(),
     582                        $embeds
     583                );
     584
     585                $mailer      = tests_retrieve_phpmailer_instance();
     586                $attachments = $mailer->getAttachments();
     587
     588                foreach ( $attachments as $attachment ) {
     589                        $inline_embed_exists = in_array( $attachment[0], $embeds, true ) && 'inline' === $attachment[6];
     590                        $this->assertTrue( $inline_embed_exists, 'The attachment ' . $attachment[2] . ' is not inline in the embeds array.' );
     591                }
     592                foreach ( $embeds as $key => $path ) {
     593                        $this->assertStringContainsString( 'cid:' . $key, $mailer->get_sent()->body, 'The cid ' . $key . ' is not referenced in the mail body.' );
     594                }
     595        }
     596        /**
     597         * Test that wp_mail() can send embedded images as a multiple line string.
     598         *
     599         * @ticket 28059
     600         * @covers ::wp_mail
     601         */
     602        public function test_wp_mail_string_embeds() {
     603                $embeds  = DIR_TESTDATA . '/images/canola.jpg' . "\n";
     604                $embeds .= DIR_TESTDATA . '/images/test-image-2.gif';
     605
     606                $message = '<p><img src="cid:0" alt="" /></p><p><img src="cid:1" alt="" /></p>';
     607
     608                wp_mail(
     609                        'user@example.org',
     610                        'Embedded images test',
     611                        $message,
     612                        'Content-Type: text/html',
     613                        array(),
     614                        $embeds
     615                );
     616
     617                $embeds_array = explode( "\n", $embeds );
     618                $mailer       = tests_retrieve_phpmailer_instance();
     619                $attachments  = $mailer->getAttachments();
     620
     621                foreach ( $attachments as $attachment ) {
     622                        $inline_embed_exists = in_array( $attachment[0], $embeds_array, true ) && 'inline' === $attachment[6];
     623                        $this->assertTrue( $inline_embed_exists, 'The attachment ' . $attachment[2] . ' is not inline in the embeds array.' );
     624                }
     625                foreach ( $embeds_array as $key => $path ) {
     626                        $this->assertStringContainsString( 'cid:' . $key, $mailer->get_sent()->body, 'The cid ' . $key . ' is not referenced in the mail body.' );
     627                }
     628        }
    557629}
Note: See TracChangeset for help on using the changeset viewer.