Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 836
PrincyV Avatar asked Dec 06 '25 17:12

PrincyV


2 Answers

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);
like image 190
PrincyV Avatar answered Dec 09 '25 15:12

PrincyV


There is this simple library: https://github.com/QualityUnit/TNEFDecoder

You can use it to easily extract attachments from TNEF files.

It is not very well documented, but it is quite simple, you should be able to use if after a little bit of digging into the code. It can be easily included using composer.

It is pure PHP implementation of TNEF decoding algorithm that was extracted from old Squirrelmail plugin and ported to be compatible with PHP 7.4

like image 30
Martin Civáň Avatar answered Dec 09 '25 15:12

Martin Civáň



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!