Make WordPress Core

Opened 11 years ago

Last modified 7 days ago

#28059 new enhancement

Inline image attachments with wp_mail()

Reported by: jesin's profile jesin Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Mail Keywords: has-patch has-test-info needs-dev-note has-unit-tests
Focuses: Cc:

Description

To insert inline images in an email the phpmailer_init action hook has to be used like this.

add_action( 'phpmailer_init', 'embed_images' );

function embed_images( &$phpmailer ) {
    $phpmailer->AddEmbeddedImage( '/path/to/image1.png', 'image1.png' );
    $phpmailer->AddEmbeddedImage( '/path/to/image2.png', 'image2.png' );
    $phpmailer->AddEmbeddedImage( '/path/to/image3.png', 'image3.png' );
}

Why not implement this in wp_mail() itself?

Attachments (2)

28059.diff (1.8 KB) - added by jesin 11 years ago.
Add inline images using wp_mail()
28059.2.diff (1.9 KB) - added by jesin 10 years ago.
Update 28059.diff

Download all attachments as: .zip

Change History (16)

@jesin
11 years ago

Add inline images using wp_mail()

#1 @jesin
11 years ago

  • Keywords has-patch needs-testing added

#2 @chriscct7
10 years ago

  • Keywords needs-refresh added

@jesin
10 years ago

Update 28059.diff

#3 follow-up: @swissspidy
10 years ago

  • Keywords needs-unit-tests added; needs-refresh removed

According to the docs, $phpmailer->addEmbeddedImage is only useful for HTML messages. What happens when you use that in a text email?

#4 in reply to: ↑ 3 @jesin
10 years ago

Replying to swissspidy:

According to the docs, $phpmailer->addEmbeddedImage is only useful for HTML messages. What happens when you use that in a text email?

In a text message the email client shows the image as an attachment. I tested this on Gmail and Outlook.com.

#5 follow-up: @chris@…
9 years ago

I'm not sure if this is needed at the wp_mail level or not (although I needed to do the workaround today) but if this gets refreshed I wouldn't blindly assume that basename( $embed ) is valid for the $cid parameter.

Most people probably have image-1.png and image-2.png but it could be http://example.com/cats/logo.png and http://example.com/dogs/logo.png and basename() would turn them both into logo.png.

This ticket was mentioned in PR #9038 on WordPress/wordpress-develop by @SirLouen.


3 weeks ago
#6

After 10 years of this patch I still find this relevant, so I'm refreshing it

Following the advice of @chrisvendiadvertisingcom, I've added an improved way to have unique cids with md5

In the next message, I will be providing some testing info

Trac ticket: https://core.trac.wordpress.org/ticket/28059

#7 in reply to: ↑ 5 @SirLouen
3 weeks ago

  • Keywords has-test-info needs-dev-note added

Testing Instructions

  1. First, you need to set up a working Mail environment:
  • You might use some env like Local WP, Laravel Herd, FlyEnv, or the liking, all come with Mail testing system
  • Or if you are using wordpress-develop you might try to configure my PR 8555 which adds Mailhog support
  1. Secondly, you need to install some WP plugin (or add some code), to connect to the email delivery server. You might use some plugins like WP Mail SMTP and configure with the hostname and port of your Mail testing service.
  1. Once you have the mailing system completely setup, it's time to use my testing plugin: https://raw.githubusercontent.com/SirLouen/wp-mail-embed-showcase/refs/tags/1.0.0/wp-mail-embed-showcase.php
  • You can check the code and adapt it to your needs, in order to add whatever extra tests you would like to do.
  • By default, it adds a menu called Mail Embed Showcase, which will send an email with two default embeds: A PNG and an SVG using the filter hook wp_mail_embed_args to specify the SVG file type image/svg+xml (not defaulted by PHPMailer).
  1. Finally, if everything is OK, you will receive an email with the two embedded images showing.

@mukesh27 commented on PR #9038:


3 weeks ago
#8

The unit tests are going failed that needs to fixed.

This ticket was mentioned in Slack in #core-test by sirlouen. View the logs.


2 weeks ago

#10 @yashjawale
2 weeks ago

Test Report

Description

This report validates whether the indicated patch works as expected.

Patch tested: https://github.com/WordPress/wordpress-develop/pull/9038.diff

Environment

  • WordPress: 6.8.1
  • PHP: 8.2.27
  • Server: nginx/1.26.1
  • Database: mysqli (Server: 8.0.35 / Client: mysqlnd 8.2.27)
  • Browser: Chrome 138.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-Five 1.2
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.2.0
    • WP Mail Embed Showcase 1.0.0 [SirLouen's testing plugin from comment:7]
    • WP Mail SMTP 4.5.0

Actual Results

  1. ✅ Able to embed the following mimetypes:
  • image/svg+xml
  • image/png
  • image/jpeg
  • image/gif
  • video/mp4
  • video/quicktime
  • audio/mpeg

Supplemental Artifacts

Before patch:
https://files.catbox.moe/6bccrs.png

After patch:
https://files.catbox.moe/wul6zc.png

#11 @iamadisingh
2 weeks ago

Test Report

Description

This report validates whether the indicated patch works as expected.

Patch tested: https://github.com/WordPress/wordpress-develop/pull/9038

Environment

  • WordPress: 6.8.1
  • PHP: 8.2.27
  • Server: nginx/1.26.1
  • Database: mysqli (Server: 8.0.35 / Client: mysqlnd 8.2.27)
  • Browser: Chrome 138.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-Five 1.2
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.2.0
    • WP Mail Embed Showcase 1.0.0
    • WP Mail SMTP 4.5.0

Actual Results

  1. ✅ Issue resolved with patch.

Additional Notes

Supplemental Artifacts

Before Patch Screenshot: https://ibb.co/pBfx4NBr
After Patch Screenshot: https://ibb.co/Z6w1ZYyW

#12 @SirLouen
2 weeks ago

  • Keywords has-unit-tests added; needs-testing needs-unit-tests removed

This ticket was mentioned in Slack in #core by sirlouen. View the logs.


8 days ago

This ticket was mentioned in Slack in #core by sirlouen. View the logs.


7 days ago

Note: See TracTickets for help on using tickets.