Attaching All Images

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
mpetrovich
Forum Commoner
Posts: 55
Joined: Fri Oct 19, 2007 2:02 am
Location: Vancouver, WA, USA

Attaching All Images

Post by mpetrovich »

I have been using the following code to attach all images to my messages and it has been working quite well with Swift Mailer.
You will need my TextBetweenArray function to grab all the image links. It has been nice to not have to click "load images" when people receive the newsletter.

Code: Select all

 
//-----Needed function: TextBetweenArray------
//-----gets all text betwen $s1 and $s2 pairs and puts in an array
function TextBetweenArray($s1,$s2,$s){
  $myarray=array();
  $s1=strtolower($s1);
  $s2=strtolower($s2);
  $L1=strlen($s1);
  $L2=strlen($s2);
  $scheck=strtolower($s);
 
  do{
  $pos1 = strpos($scheck,$s1);
  if($pos1!==false){ 
    $pos2 = strpos(substr($scheck,$pos1+$L1),$s2);
    if($pos2!==false){
      $myarray[]=substr($s,$pos1+$L1,$pos2);
      $s=substr($s,$pos1+$L1+$pos2+$L2);
      $scheck=strtolower($s);
      }
        }
  } while (($pos1!==false)and($pos2!==false));
 
  return $myarray;
}
 
//-------ATTACH ALL IMAGES-------
//-------$htmlcontent is the message text in HTML format
//-------Images are in an <img> tag using a relative link starting with a '/' such as '/images/myimage.jpg'
    $ROOT = $_SERVER['DOCUMENT_ROOT'];
    $ImageArray = TextBetweenArray('src="','"',$htmlcontent);
    foreach($ImageArray as $image){
      $img  =& new Swift_Message_Image(new Swift_File("$ROOT$image"));
      $src  = $message->attach($img);
      $htmlcontent = str_replace($image,$src,$htmlcontent);
    }
 
EoN
Forum Newbie
Posts: 22
Joined: Thu Oct 11, 2007 5:57 am

Re: Attaching All Images

Post by EoN »

mpetrovich wrote:It has been nice to not have to click "load images" when people receive the newsletter.
Hi mpetrovich! :)

Why don't users have to click 'load images' when receiving your newsletter?

As far as I knew, Hotmail, Gmail, Yahoo, Outlook etc ALL block ANY images by default these days unless the user explicitly sets otherwise?
mpetrovich
Forum Commoner
Posts: 55
Joined: Fri Oct 19, 2007 2:02 am
Location: Vancouver, WA, USA

Re: Attaching All Images

Post by mpetrovich »

Why don't users have to click 'load images' when receiving your newsletter?
As you know, most email/Webmail programs now have a function to prevent remote images from loading automatically. The purpose of this is to prevent unwanted verification of your email address. Some Spammers used to use a 1 pixel gif with a code as a flag. When the images are retrieved from the server, the sender can track who is getting the image and then verify the email that the messages was sent to. If you attach all your images, then they are safe and they are automatically displayed in most programs (unless you have set your options to be plain text).

By the way, this is one of the reasons I like Swift Mailer. In phpList, I could attach the template images, but I could not attach images within the message. I use TinyMCE for the user HTML message creation, and if they upload images, the above mentioned code will attach them all.
EoN
Forum Newbie
Posts: 22
Joined: Thu Oct 11, 2007 5:57 am

Re: Attaching All Images

Post by EoN »

Hi mpetrovich,

Thanks for your reply!
mpetrovich wrote:
Why don't users have to click 'load images' when receiving your newsletter?
As you know, most email/Webmail programs now have a function to prevent remote images from loading automatically. The purpose of this is to prevent unwanted verification of your email address. Some Spammers used to use a 1 pixel gif with a code as a flag. When the images are retrieved from the server, the sender can track who is getting the image and then verify the email that the messages was sent to. If you attach all your images, then they are safe and they are automatically displayed in most programs (unless you have set your options to be plain text).

By the way, this is one of the reasons I like Swift Mailer. In phpList, I could attach the template images, but I could not attach images within the message. I use TinyMCE for the user HTML message creation, and if they upload images, the above mentioned code will attach them all.
Ahh yes, I understand the tracking remote image situation. But as far as I know, unfortunately, all the new versions of email clients do hide embedded images as well as remote ones by default. I don't understand WHY they do this, but they do. To me it would only seem necessary to hide remote images, for the reasons you mentioned, but it seems they're paranoid now. Paranoid or just stupid.

When I tested embedded images sent to hotmail, gmail, yahoo & outlook, every single one of them seemed to hide the images by default - until you click 'display images'.

Unless I'm doing something wrong!? Can anyone please confirm?!?!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Attaching All Images

Post by Chris Corbyn »

I'm not aware of any mail client I use hiding embedded images. They're safe. Just referenced internal to the overall message structure.

Unless perhaps disabling the display of attachments inline causes them to be hidden.
EoN
Forum Newbie
Posts: 22
Joined: Thu Oct 11, 2007 5:57 am

Re: Attaching All Images

Post by EoN »

This is a situation I would LOVE to find out that I am wrong! My whole purpose of originally switching to embedded was to try to prevent initial image-hiding. But every email client I've seen in the past 1-2 years DOES hide embedded images by default. I'm talking about new versions here - Outlook 2007, Windows Live, Gmail, Yahoo etc. This is with DEFAULT email client settings (no extra tweaking/security/customisation).

I'd love to hear some further opinions/experiences with this. You guys want to test your embedded image emails again on a brand new email account? If it's something I'm doing wrong, I'd love to know - I'm going to investigate my tests/set up again and see if I've messed up something.

Is it possible you guys had the images hidden the first time, then clicked "always display images" which made every email from that point onwards display them?
Chris Corbyn wrote:Unless perhaps disabling the display of attachments inline causes them to be hidden.
Do you mean as an email client setting, or a Swift setting? If the latter, where is this done?

( PS. - Thanks for a great thread. I've said it before, and I'll say it again, i reckon Swift is one of the best products out there, with one of the best support forums I've seen. Keep up the good work! )
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Attaching All Images

Post by Chris Corbyn »

I was referring to email client settings, though I've only seen that setting in Thunderbird in reality.

I recently finished the MIME library from v4 (which is "hot" by the way :P) and managed to get images embedded in Apple Mail, Thunderbird and Entourage (the mac equivalent of Outlook) successfully. Granted, all I can do right now is save the message source as .eml on disk and open that eml in the software. SMTP should be testable tonight since the only bits thats missing is the socket layer (yay for TDD allowing me to develop in weird ways :P). I'll do some further testing on this once SMTP in v4 works.
Post Reply