Rotating Images In Emails

Probably got my description wrong, but I would like to be able to insert a placeholder into emails that serve a random image from a web folder, so that every time the email (and all emails) are opened a different image is served.

I suppose it is a bit like Google ad placement or rotating banner on a web page.


I have seen a couple code examples, but have not managed to get it to work.
 

fisicx

Moderator
Sep 12, 2006
46,673
8
15,362
Aldershot
www.aerin.co.uk
Inline images need base64 encoding if you want them to be seen. If you insert an image with an src address it will as @MikeJ suggests be blocked.

It is possible to do as you want but it's not easy. It all depends on how your emails are assembled.
 
Upvote 0
My first thought was using an open source ad server, but as you had to insert some (java?) script in the code, it was going to be a nogoer.

I remember that I do have a service somewhere that allows you to embed videos into email, which was pretty successful.

The email services I want to use are my normal Gmail/domain account and email newsletter system.

I will also see if I can dig out the code I was told would work....
 
Upvote 0
Ok, I have something that works a little:


Create a php file and add to your server:

<?php
$images = [
'https://example.com/image1.jpg',
'https://example.com/image2.jpg',
'https://example.com/image3.jpg'
];
$randomImage = $images[array_rand($images)];
header("Content-Type: image/jpeg");
readfile($randomImage);
?>


Add the image link to your email
<img src="https://yourserver.com/path/to/your/script.php" alt="Dynamic Image">

Now, I have found out that Google caches email images, so it causes issues, so came across this as a potential solution:
Add the following to your email header:

header('Content-Type: image/jpeg');
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

However, I do not think it is working.

When I open an email with the above in Gmail PC browser an image is shown, but cannot get it to rotate. Open in Gmail Android app and I get the header code and link to image - click the link and a different image is shown!


I will keep trying!
 
Upvote 0

fisicx

Moderator
Sep 12, 2006
46,673
8
15,362
Aldershot
www.aerin.co.uk
And in all sorts of email clients external images will get blocked. Which is why they need encoding.
 
Upvote 0
I will look at base64, but understand that can bloat the message.
 
Upvote 0

fisicx

Moderator
Sep 12, 2006
46,673
8
15,362
Aldershot
www.aerin.co.uk
Upvote 0

fisicx

Moderator
Sep 12, 2006
46,673
8
15,362
Aldershot
www.aerin.co.uk
Will it work doing it the other way?

Insert the image in to the email from a url then rename the uploaded images on the server
Still doesn't change the fact that some email clients and people with higher levels of security won't see the image. Desktop clients are the worst.
 
Upvote 0

Latest Articles

Join UK Business Forums for free business advice