Can you suggest a more elegant solution?

1. Don't. A sane mail client won't load remote images, so tracking won't work.

2. Don't generate the same constant image over and over again. Use any tool you like to create the transparent pixel image as GIF or PNG once. I used Google ("transparent pixel gif") and found this page. It lists the resulting binaries conveniently as base64 strings. For a transparent pixel in GIF format, it is R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==, in PNG, it is iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII=. Now you can use MIME::Base64 or similar to convert that to a binary:

use MIME::Base64 qw( decode_base64 ); # ... print "Content-type: image/gif\n\n"; binmode STDOUT; print decode_base64('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAA +AICRAEAOw==');

Of course, you could even get rid of MIME::Base64 by recoding that to hex, and use pack to write the binary without any extra modules:

>perl -MMIME::Base64 -e 'print decode_base64("R0lGODlhAQABAIAAAP///wAA +ACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")' > pixel.gif >perl -MMIME::Base64 -e 'print unpack("H*",decode_base64("R0lGODlhAQAB +AIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="))' 47494638396101000100800000ffffff00000021f90401000000002c00000000010001 +000002024401003b >perl -e 'print pack("H*","47494638396101000100800000ffffff00000021f90 +401000000002c00000000010001000002024401003b")' > pixel2.gif >md5sum pixel.gif pixel2.gif 325472601571f31e1bf00674c368d335 pixel.gif 325472601571f31e1bf00674c368d335 pixel2.gif >
Like this:
print "Content-type: image/gif\n\n"; binmode STDOUT; print pack('H*','47494638396101000100800000ffffff00000021f904010000000 +02c00000000010001000002024401003b');

You could even get rid of pack:

>perl -MMIME::Base64 -MData::Dumper -e '$x=decode_base64("R0lGODlhAQAB +AIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="); $Data::Dumper::Us +eqq=1; print Dumper($x);' $VAR1 = "GIF89a\1\0\1\0\200\0\0\377\377\377\0\0\0!\371\4\1\0\0\0\0,\0\ +0\0\0\1\0\1\0\0\2\2D\1\0;"; >

Et voilą:

print "Content-type: image/gif\n\n"; binmode STDOUT; print "GIF89a\1\0\1\0\200\0\0\377\377\377\0\0\0!\371\4\1\0\0\0\0,\0\0\ +0\0\1\0\1\0\0\2\2D\1\0;";

(Note: That assumes an ASCII-based system. EBCDIC probably won't work with that constant.)

And there is more: You can omit binmode on Unix, as Unix is 8-bit-clean. On DOS-based systems (DOS, OS/2, Windows), binmode prevents converting \n = \012 = 0x0A to \r\n = \015\012 = 0x0D, 0x0A. THIS GIF luckily contains no \n = 0x0A, so it does not matter if \n is converted or not. So for this constant, you can omit binmode also on DOS, OS/2, Windows:

print "Content-type: image/gif\n\nGIF89a\1\0\1\0\200\0\0\377\377\377\0 +\0\0!\371\4\1\0\0\0\0,\0\0\0\0\1\0\1\0\0\2\2D\1\0;";

Yipp, a single line of code works on Unix, DOS, OS/2, and Windows.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re: Create email tracking image by afoken
in thread Create email tracking image by Bod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.