in reply to Create email tracking image
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:
Like this:>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 >
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Create email tracking image
by Bod (Parson) on Mar 21, 2023 at 22:34 UTC | |
by bliako (Abbot) on Mar 22, 2023 at 13:32 UTC |