Skip to content

Instantly share code, notes, and snippets.

@majick777
Created February 22, 2019 06:32
Show Gist options
  • Save majick777/0c4c8be15b1c05c6fdea1c70e0687bbb to your computer and use it in GitHub Desktop.
Save majick777/0c4c8be15b1c05c6fdea1c70e0687bbb to your computer and use it in GitHub Desktop.
<?php
// ------------------------
// Email Image AutoEmbedder
// ------------------------
add_action('phpmailer_init', 'email_images_embedder');
function email_images_embedder($phpmailer) {
// --- bug out if just text body ---
$content_type = $phpmailer->ContentType;
$text_types = array('text/plain', 'text', 'txt', 'plaingtext');
if (in_array($content_type, $text_types)) {return;}
// --- get the HTML message body ---
$message_body = $phpmailer->Body;
// --- search for image tag matches ---
$pattern = '/<*img[^>]*src *= *["\']?([^"\']*)/i';
preg_match_all($pattern, $message_body, $found_images);
$imageurls = $found_images[1];
// --- bug out if no image matches found ---
if (count($imageurls) == 0) {return;}
// --- remove duplicates for processing ---
$imageurls = array_unique($imageurls);
// --- get uploads base path ---
$siteurl = site_url();
$uploads = wp_upload_dir(null, false); // (wp_get_upload_dir)
$paths['uploads']['dir'] = $uploads['basedir'];
$paths['uploads']['url'] = $uploads['baseurl'];
// --- get possible plugin settings ---
// (can modify prefixes to match plugin options)
$images_url = get_option('email_images_url');
$images_dir = get_option('email_images_dir');
if ($images_url && $images_dir) {
$paths['settings']['dir'] = $images_dir;
$paths['settings']['url'] = $images_url;
}
// --- filter paths for custom usage ---
$paths = apply_filters('email_image_paths', $paths);
// --- loop found images ---
$j = 0; $uids = array();
foreach ($imageurls as $i => $url) {
// --- generate unique image ID ---
$uid = 'image'.rand(0, 999999);
if (in_array($uid, $uids)) {
while (in_array($uid, $uids)) {
$uid = 'image'.rand(0, 999999);
}
$uids[] = $uid;
}
// --- check in upload and image directories ---
$filepath = false;
foreach ($paths as $pathname => $path) {
$checkpath = str_replace($path['url'], $path['dir'], $url);
if (file_exists($checkpath)) {$filepath = $checkpath; break;}
}
// --- maybe attempt manual base path matching ---
if (!$filepath) {
if (strpos($url, $siteurl) === 0) {
$checkpath = str_replace($siteurl, ABSPATH, $url);
if (file_exists($checkpath)) {$filepath = $checkpath;}
}
}
// --- set image data ---
if ($filepath) {
$images[$j] = array(
'url' => $url,
'uid' => $uid,
'filepath' => $filepath,
'filename' => basename($filepath)
);
$j++;
}
}
// --- loop to add embedded images ---
foreach ($images as $j => $image) {
// --- replace image URL with cid in message body ---
$message_body = str_replace($image['url'], "cid:".$image['uid'], $message_body);
// --- embed image via phpmailer ---
$phpmailer->AddEmbeddedImage($image['filepath'], $image['uid'], $image['filename']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment