Help fight hotlinking
M
MadMax∞
MailPoet is great by allowing to use any WordPress or WooCommerce ressource in your website, but the MAJOR drawback of having a WordPress integrated newsletter solution like MailPoet is hotlinking!
As you need customers to view your emails images, and as MailPoet do not embed them in the email (which is great!), you live a hard time fighting hotlinking as every directory must accessible to any webmail domain your customers may use!
But if MailPoet could copy any image used in an email under a specific directory (even better if in subdir of this directory) prior to sending it, webmasters would just have to allow free access to this directory.
Everything else could be protected against hotlinking!
In the way I see it for now, MailPoet is a real SEO killer to websites as a consequence of that hotlinking issue that allows toxic backlinks you then need to disavow in Google Search Console...
I really wish you could add a new feature to do so (copy files in a different directory).
As a second gain, your emails woule remain visible and accurate as long as you keep the email specific directory. Even if you delete the original image (or product for that matter if you use WooCommerce)!
I hope you'll be interested in such a feature.
M
MadMax∞
I’ve worked on this subject (important one for us) and already found a way to deal with this.
I used a filter MailPoet offers to do so. It makes no change for newsletter editor but make copy of images and changes the images URL accordingly at render time (so before the mail is actually sent).
I share it here (you may need to adapt here to fit your needs):
add_filter('mailpoet_sending_newsletter_render_after_pre_process', 'mr_process_mail', 10, 2);
function mr_process_mail ($aRenderedNewsletter, $oNewsletter) {
$aRenderedNewsletter['html'] = mr_process_images( $aRenderedNewsletter['html'], $oNewsletter->getId() );
return $aRenderedNewsletter;
}
function mr_process_images( $sHtml, $iId ) {
// CREATE DIRECTORY
$sDestDir = '/wp-content/uploads/mailpoet/newsletter_images/' . $iId;
if ( !file_exists( ABSPATH . $sDestDir ) ) mkdir ( ABSPATH . $sDestDir, 0755, true );
$sDetectionRegExp = '#(<img[^/>]+)(?<=\s)(src=)(\"|\')([^\"\']
)(\'|\")([^/>]
/>)#miUs';$sResult = preg_replace_callback( $sDetectionRegExp, function( $matches ) use( $sDestDir ) {
// COMPUTE URI
$src = $matches[4];
$new_src = preg_replace( '#(.
)/wp-content/(.
)/([\w-]+)(\.(jpe?g|png|gif|webp|avif|svg))$#iU', '$1'.$sDestDir.'/$3$4', $src );$new_src = preg_replace( '#(.
)/wp-content/(.
)/([\w-]+)(\.(jpe?g|png|gif|webp|avif|svg))\?(.*)$#iU', '$1'.$sDestDir.'/$3$4?$6', $new_src );// COMPUTE PATHS
$site_url = get_site_url(null, '/');
$src_file = preg_replace('' . $site_url . '', ABSPATH, $src);
$src_file = preg_replace('\?(.*)$', '', $src_file); // Remove query string
if ( file_exists( $src_file ) ) {
$dst_file = preg_replace('' . $site_url . '', ABSPATH, $new_src);
$dst_file = preg_replace('\?(.*)$', '', $dst_file); // Remove query string
// COPY FILES
if ( file_exists( $dst_file ) ) unlink( $dst_file );
if ( ( $in = fopen( $src_file, "rb" ) ) && ( $out = fopen( $dst_file, "wb" ) ) ) {
while ( !feof( $in ) ) {
$buffer = fread( $in, 64 );
fwrite( $out, $buffer );
}
fclose( $out );
fclose( $in );
return $matches[1].$matches[2].$matches[3].$new_src.$matches[5].$matches[6];
}
return $matches[0]; // No change
}
return $matches[0]; // No change
}, $sHtml );
return $sResult;
}
Thank you for this great plugin!