Embedding images in phplist messages

From phplist version 2.10.8 (maybe from some version earlier), there's the possibility to embed images directly into the email messages, avoiding the usage of the external link. You have just to put the following statement into config/config.php:

# Aggiunta di Gabo per permettere di inserire le immagini nel corpo del messaggio
define("EMBEDUPLOADIMAGES",1);

The problem was that it didn't work for me. No way. So I had a look into the code and I found the problem. There where no way to embed images stored in any subdirectory of what stated in:

# If you want to upload images to the FCKeditor, you need to specify the location
# of the directory where the images go. This needs to be writable by the webserver,
# and it needs to be in your public document (website) area
# the directory is relative to the root of PHPlist as set above
# This is a potential security risk, so read README.security for more information
define("FCKIMAGES_DIR","uploadimages");

Substituting filesystem_image_exists() and get_filesystem_image() functions (both in admin/class.phplistmailer.php) whith the code below worked, allowing me to embed images in mails even if them are in a directory different from uploadimages.

First of all, I added the following to the config file, just to permit embedding images when sending through cronjob and PHP-cli:

//ADDED BY GABO
$phplistRoot = '/home/mysite/public_html';
//END ADDED BY GABO
[/code]

Ecco le funzioni modificate:

[code lang="php"]
## addition for filesystem images - MODIFYED BY GABO
    function filesystem_image_exists($filename) {
      ##  find the image referenced and see if it's on the server

      $elements = parse_url($filename);
      $localfile = basename($elements['path']);

      // search for subdirectory
      $basePath = $GLOBALS['phplistRoot'].$GLOBALS['pageroot'].'/'.FCKIMAGES_DIR;
      $dir = substr($elements['path'],
                    strpos($elements['path'],FCKIMAGES_DIR)+strlen(FCKIMAGES_DIR),
                    strlen($elements['path']) -
                       strpos($elements['path'],FCKIMAGES_DIR) -
                       strlen(FCKIMAGES_DIR)-strlen($localfile));

      return is_file($basePath.$dir.$localfile);
    }

    function get_filesystem_image($filename) {
      ## get the image contents
      $elements = parse_url($filename);
      $localfile = basename($elements['path']);

      $basePath = $GLOBALS['phplistRoot'].$GLOBALS['pageroot'].'/'.FCKIMAGES_DIR;
      $dir = substr($elements['path'],
                    strpos($elements['path'],FCKIMAGES_DIR)+strlen(FCKIMAGES_DIR),
                    strlen($elements['path']) -
                       strpos($elements['path'],FCKIMAGES_DIR) -
                       strlen(FCKIMAGES_DIR)-strlen($localfile));

      if (is_file($basePath.$dir.$localfile)) {
        return base64_encode( file_get_contents($basePath.$dir.$localfile) );
      }
      return 0;
    }
    ## end addition - MODIFYED BY GABO

Hope it helps saving time to someone :)

5 Comments to “Embedding images in phplist messages”

  1. mystic says:

    Thanks – that helps.

    But for newbies, might mention exactly how to insert the image after making the changes described above…?

    This seemed to work: edit the HTML for the image being sent, and ensure its “src” attribute is set to the full filesystem path to the image file.

    A broken-image icon appears on the editor, but the image seems to be sent just fine!

  2. mystic says:

    (Thanks for the code – your blog displays a strange ‘ character that can’t be easily copied and pasted, unfortunately!)

    • gabo says:

      > But for newbies, might mention exactly how to insert the image after making the changes described above…?

      For that information newbies can read phplist documentation and forum, that’s an advanced hack to that software, not a simple script-kiddie cut/paste patch. I think that if someone is reading this post already knows where to insert the images..

      > (Thanks for the code – your blog displays a strange ‘ character that can’t be easily copied and pasted, unfortunately!)

      Thanks for the reporting, I will have a look at the plugin-code, it also generate non-XHTML strict code…

  3. Leonardo says:

    Hey, it worked for me.

    But that fucking ‘ was very hard to see.

Leave a comment