Search code examples
phplinuximaptnefwinmail.dat

How to extract TNEF winmail.dat using a PHP script?


I am developing a functionality that will read all emails from a mailbox and will process the attachment files.

When any mail is sent from Outlook, its attachment is saved as winmail.dat. [Please note that I already have code to download attachments using IMAP PHP. My email attachments are stored in one folder in my codebase on the server.] I need to extract the winmail.dat attachment into its original format. The solutions I came across till now all need manual efforts like an online tool which gives extracted files, or software that enables us to convert .DAT files.

But I need to extract using a script or any automated process like cron or something. [I am using Linux - centos 5.7 server.]

Is there any library or any other way for my application to "unpack" theses files so I can get the attachments WITHOUT doing it MANUALLY one by one?

Thanks!


Solution

  • Download rpm package from http://pkgs.org/centos-5-rhel-5/flexbox-x86_64/tnef-1.4.7-1.x86_64.rpm.html

    use Package: tnef-1.4.7-1.x86_64.rpm

    Installation:

    Copy into a folder and Install rpm tnef package -

    rpm -ivh /path-of-downloaded-package/tnef-1.4.7-1.x86_64.rpm
    

    To Extract attachment [winmail.dat] :

    Go in directory where you want to extract your attachment files.

    cd /path-to-extract-attachment/
    

    run command:

    tnef /path-of-attachment/winmail.dat
    

    Equivalent PHP script:

    chdir('/path-to-extract-attachment/');
    
    $cmd = "tnef /path-of-attachment/winmail.dat";
    shell_exec($cmd);