1

I want to send some newsletter, and I composed an HTML email with an image in the middle. If I use the absolute path to the image, if I replace that image in the server, old emails will get updated as well (as they are a reference to the absolute path). Instead, I would like something like a relative path or somehow embed the image in that part of the email (not as an attachment), so that the email is static and even if I replace that image for next month, it won't affect.

I want to keep the same filename for everymonth so that I can simply replace the image by another with the same name and resend the email with PHPMailer.

Also, if I embed the image instead of adding the full path, it makes it more secure since I don't want anyone to access the image with the link, only if they received the email. If I give them the full path someone could actually distribute the image and they would get free image every month since I Would be updating it in the same URL.

Is there any workaround or solutiong for that?

Daniel Roca
  • 35
  • 1
  • 5
  • 1
    `If I give them the full path someone could actually distribute the image and they would get free image every month`... It's no different if you embed the image though, people can still copy it – ADyson Dec 27 '21 at 11:35
  • I'm specially concerned about the fact that referencing the full path means if I change that image, all the old emails will get updated too – Daniel Roca Dec 27 '21 at 11:43
  • Ok well I agree that's the main issue, and the link I gave you should remove that scenario. – ADyson Dec 27 '21 at 11:55
  • @ADyson that link offers to use Cid but if you check the support for CID it's actually not that much – Daniel Roca Dec 27 '21 at 11:57
  • Well that's your option. Either that or don't keep re-using the same URL each week in your email - just create a new image with a new URL in each new email, then previous ones are not affected – ADyson Dec 27 '21 at 12:01
  • Cids are universally supported, but they won’t help you at all because they are only relevant to embedded images, not remote ones. Do what @ADyson said. – Synchro Dec 27 '21 at 17:42

1 Answers1

0

You wrote: "or somehow embed the image in that part of the email (not as an attachment), so that the email is static and even if I replace that image for next month, it won't affect."

If you don't want to embed it as attachment and link to it, let's embed the image into mail body without attachment.

At first get the base64 string of your image:

$img=file_get_contents("some_image.jpg");
echo base64_encode($img);

You will see the base64 encoded string of your image something like 'QSkZJRgABAgEAYABgA....', the just put that string into you email body:

$mail_body="
<html>
<head>
</head>
<body>

bla bla bla

<img src="data:image/jpeg;base64,QSkZJRgABAgEAYABgA....." />

bla bla bla

</body>
</html>";

If you are using 'phpmailer' for sending emails, just don't forget to add the following parameters in phpmailer:

$mail->Body = $mail_body;
$mail->IsHTML(true);
Developer
  • 36
  • 3