Opened 11 years ago
Last modified 7 days ago
#28059 new enhancement
Inline image attachments with wp_mail()
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | 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)
Change History (16)
#3
follow-up:
↓ 4
@
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
@
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:
↓ 7
@
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
@
3 weeks ago
- Keywords has-test-info needs-dev-note added
Testing Instructions
- 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
- 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.
- 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 typeimage/svg+xml
(not defaulted by PHPMailer).
- 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
@
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
- ✅ Able to embed the following mimetypes:
-
image/svg+xml
-
image/png
-
image/jpeg
-
image/gif
-
video/mp4
-
video/quicktime
-
audio/mpeg
Supplemental Artifacts
#11
@
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
- ✅ Issue resolved with patch.
Additional Notes
- Activated the wp-mail-embed-showcase.php plugin mentioned by @SirLouen https://raw.githubusercontent.com/SirLouen/wp-mail-embed-showcase/refs/tags/1.0.0/wp-mail-embed-showcase.php
- Modified the plugin code to check for
image/gif
- Able to embed following mime types:
image/gif
image/png
image/svg+xml
Supplemental Artifacts
Before Patch Screenshot: https://ibb.co/pBfx4NBr
After Patch Screenshot: https://ibb.co/Z6w1ZYyW
Add inline images using
wp_mail()