Damibu
Forum Replies Created
Viewing 2 replies - 1 through 2 (of 2 total)
-
Forum: Plugins
In reply to: [Mail Queue] multipart/mixed eMails for Embedded ImagesHi,
I got it working like this….
- Add a global phpmailer_init that checks if emails have this header and does the $phpmailer->addEmbeddedImage
- Add an array of cid=>file_paths in a new mail header called X-Embedded-Images
- Call wp_mail as normal
Details
X-Embedded-Images Header Setup
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($email_html);
libxml_clear_errors();
$xpath = new DOMXPath($dom);
// Find all the imgs and prepare them for CID embedding
$images = $xpath->query('//img');
$embedded_images = [];
foreach ($images as $img) {
$src = $img->getAttribute('src');
$parsed_url = parse_url($src);
$path = ABSPATH . substr($parsed_url['path'], strlen('/wordpress/'));
$cid = basename($path);
$img->setAttribute('src', 'cid:' . $cid);
$embedded_images[] = [
'path' => $path,
'cid' => $cid
];
}
$email_html = $dom->saveHTML();
// Store embedded images data in a custom header that survives the queue
if ( !empty($embedded_images) ) {
$embedded_images_data = base64_encode(serialize($embedded_images));
$headers[] = "X-Embedded-Images: {$embedded_images_data}";
}phpmailer_init action is…
add_action('phpmailer_init', 'reconstruct_embedded_images', 10);
static function reconstruct_embedded_images($phpmailer) {
// Check if this email has embedded images stored in headers
$custom_headers = $phpmailer->getCustomHeaders();
foreach ($custom_headers as $header) {
if (strpos($header[0], 'X-Embedded-Images') === 0) {
// Decode the embedded images data
$embedded_data = unserialize(base64_decode($header[1]));
if (is_array($embedded_data)) {
foreach ($embedded_data as $image) {
if (isset($image['path']) && isset($image['cid']) && file_exists($image['path'])) {
$phpmailer->addEmbeddedImage($image['path'], $image['cid']);
}
}
}
// Remove the custom header so it doesn't appear in the final email
$phpmailer->clearCustomHeaders();
// Re-add all other custom headers except the X-Embedded-Images one
foreach ($custom_headers as $other_header) {
if (strpos($other_header[0], 'X-Embedded-Images') !== 0) {
$phpmailer->addCustomHeader($other_header[0], $other_header[1]);
}
}
break;
}
}
}Forum: Plugins
In reply to: [Mail Queue] multipart/mixed eMails for Embedded Imageshappy to help 🤣
solution could be your own hook, although I think you’re trying not to do that. Or a hackie way could be a naming convention on an attachment
cheers
Dave
Viewing 2 replies - 1 through 2 (of 2 total)