## TnefDecode Mailtraq Message Filter (perl)

This forum is for discussing Mailtraq's Scripting features. Get help with Mailtraq scripts, templates and external ActiveX scripting.

### TnefDecode Mailtraq Message Filter (perl)

TnefDecode decodes Microsoft's proprietary TNEF mime (typically, containing winmail.dat) and adds the results as standard mime attachments - so that any mail clients can access the message contents. The script operates as a [url=http://www.mailtraq.com/291/]Mailtraq Message Filter[/url] where Mailtraq hands-off all inbound messages to a specified directory - the 'InQueue' - and picks up any message files passed to an 'OutQueue'. TnefDecode.pl's job is to pass messages from InQueue to OutQueue, converting any TNEF along the way. The script runs as a Win32 service, polling the InQueue every 2 seconds, by default.

To install:

1. You'll need Perl running on the Mailtraq machine. I've been using 5.6.1 from http://www.activestate.com. Update the machines 'path' environment var to include the perl executables' location, if not already set. The default installation includes the Perl Package Manager (PPM) and quite a few of the modules needed to run the script.

2. Install the following perl modules. To use the PPM enter "PPM" from the command line, then run the six commands:
Code: Select all
ppm> install MIME-toolsppm> install TNEF-Convertppm> install MIME-Typesppm> install Getopt-Longppm> install http://www.roth.net/perl/packages/win32-daemon.ppdppm> install http://www.roth.net/perl/packages/win32-perms.ppd
The last two provide the service handling features (See [url]http://www.roth.net/perl/scripts/[/url]).

3. Set the Mailtraq Message Filter registry locations. Shut down Mailtraq and update this registry key:
Code: Select all
LocalMachine/Software/Fastraq/Mailtraq/MessageFilters/TnefDecode

The key must contain 'InQueue' and 'OutQueue' string values with the full path to the input and output directories, e.g:
Code: Select all
InQueue="C:\Mailtraq\database\TnefDecode\In"OutQueue="C:\Mailtraq\database\TnefDecode\Out"

4. Install the script as a Win32 service. Copy the script to, say, C:\Mailtraq\database\TnefDecode\ then install the service from the command line with the following parameters:
Code: Select all
perl tnefdecode.pl -install -a -hdrs -m

When first run you'll see messages about some working directories being created (these exist at the same level as 'InQueue').

-a writes ".activate", a zero-byte file to InQueue at every service start-up. This ensures that Mailtraq uses the Message Filter - without it, Mailtraq will ignore the filter and process normally.

-hdrs will delete any MS-TNEF-Correlator header and add an X-Perl-TnefDecode header to any messages that have been decoded. The latter is also used to show any unsuccessful attempts at mime decoding.

-m will output a file to the OutQueue on TNEF decoding so that Mailtraq's log file includes entries like these:
Code: Select all
00010000 00000000 [timestamp] MessageFilter: TnefDecode {filename} {n} TNEF attachments decoded00000800 00000000 [timestamp] MessageFilter: TnefDecode {filename} can't decode mime structure

-f is used to change the InQueue polling frequency from the default 2 seconds.

-l allows you to change the log file location (it lives above InQueue by default).

-remove will uninstall the win32 service.

More information is available at the command line - run the script with "-h" for 'help' (perl tnefdecode.pl -h).

5. Start the TnefDecode service.
From the command line run 'net start tnefdecode' or you can 'Start/Stop/Pause/Resume' from the windows Service Control Manager.
It's probably worth excluding the TnefDecode directories from any active virus scanning - I'm not sure how the script would react if messages were locked or hoisted away mid-session.

When the service starts it should create a log file which records service events and conversion activity (successes and failures). If the script finds a message with mime that it can't handle, a copy will be stored in an "unparsable-mime" directory (located at the same level as InQueue) before it's passed to OutQueue. I've been running for a good few months and I've only found 1 so far (a message containing an attachment with a Hungarian file name).

Cheers, Martin
Attachments
TnefDecode.zip
Contains TnefDecode.pl 20071118

Martin Clayton
Expert User

Posts: 529
Joined: Sat Jan 15th, 2005 8:20am
Location: London, UK

### Re: TnefDecode Mailtraq Message Filter (perl)

Martin Clayton wrote:TnefDecode decodes Microsoft's proprietary TNEF mime
Thanks for posting this, Martin. I thoroughly enjoyed reading someone else's code for a change and I particularly liked the way you managed to implement Dave Roth's Win32::Daemon - I can never get my head around any of his mazy modules so I avoid them wherever possible. I'll study it all in more detail later and comment off-list (nothing serious, a few unperlish lines jumped out at me). Thanks again.
jimhill
Expert User

Posts: 337
Joined: Sun Dec 19th, 2004 9:59pm
Location: UK

### Re: TnefDecode Mailtraq Message Filter (perl)

jimhill wrote:
Martin Clayton wrote:TnefDecode decodes Microsoft's proprietary TNEF mime
I thoroughly enjoyed reading someone else's code for a change

Great, but I hope that it wasn't too amusing. ;-)

jimhill wrote:I'll study it all in more detail later and comment off-list (nothing serious, a few unperlish lines jumped out at me).

Champion, thanks. I was in trial and error mode most of the time and some aspects still bug me. Bit hazy now but ... did the conversion counter really need to be passed as a reference? The destruction of the original tnef mime parts felt a bit clumsy but I wanted to avoid writing a new message and couldn't find any examples of TNEF::Convert modifying an input message rather than building a new mime entity. Anyway, your comments are more than welcome, no matter how trivial (not the 'comment', the 'issue'!).

Martin Clayton
Expert User

Posts: 529
Joined: Sat Jan 15th, 2005 8:20am
Location: London, UK

### Re: TnefDecode Mailtraq Message Filter (perl)

Hi Martin,
Hopefully you can help me here. I have install perl and ran all the install modules ok.
Then add the keys into registry using c:\mailbox\in and c:\mailbox\out.
I have created the 2 directories above.
When I run the script (saved in c:\mailbox) it comes up with directory not found: inqueue="c:\mailbox\in"
couldn’t create directory: inqueue="c:\mailbox\in" - invalid argument at tnefdecode.pl line 533.
I have double checked and the directories in the registry definitely match the actual directories.
I have deleted the in and out directory thinking the script would make them but still the same error.
Permissions for read/write are fully open on the directory.
Any help will be greatly appreciated.
nezzal

Posts: 4
Joined: Wed Aug 13th, 2008 8:03pm

### Re: TnefDecode Mailtraq Message Filter (perl)

Hi nezzal,

Sorry for the delay - I'm just back from a short holiday.

nezzal wrote:When I run the script (saved in c:\mailbox) it comes up with directory not found: inqueue="c:\mailbox\in"
couldn’t create directory: inqueue="c:\mailbox\in" - invalid argument at tnefdecode.pl line 533.
I have double checked and the directories in the registry definitely match the actual directories.

I suspect that the registry entries are the problem... the LocalMachine/Software/Fastraq/Mailtraq/MessageFilters/TnefDecode key should contain two string values: InQueue and OutQueue - these 'values' only contain the path information. A regedit export should produce something like the following:

Code: Select all
[HKEY_LOCAL_MACHINE\SOFTWARE\Fastraq\Mailtraq\MessageFilters\TnefDecode]"OutQueue"="C:\\mailbox\\out""InQueue"="C:\\mailbox\\in"

If that doesn't help, please post a regedit export, name your OS & perl version - and I'll look deeper...

Martin Clayton
Expert User

Posts: 529
Joined: Sat Jan 15th, 2005 8:20am
Location: London, UK

Hi Martin