Script to Remove Attachments in Exchange Mailbox

K

KDawg44

Hi,

I am frustrated with my users who send large files around the office
instead of using the network shares.

For instance, this is one of many emails I have sent around:

"If you take the this kit it looks like J sent it to D, D sent it to
you, then you sent it to me. The problem with that is just that now
there are copies of this file:


On J's drive or Comm drive
J's sent items
D's inbox
D's sent items
Your inbox
Your sent items
My inbox
The comm. Drive once I save it there to be used.

Which means that there are 8 copies of the same file 4MB taking up
space, or a 4MB file turned into a 32MB file."

My users just aren't getting it! And then they complain when they get
quota messages! (and some people, when they are Important People, you
cannot argue with).

So, what I would like, is to write a script that parses the exchange
mailbox, and removes all attachments over a certain size (say 500K)
that are attached to messages that are more than 2 weeks old, or that
are in sent items. I would like to write to a log file all the
changes that are made then email that log file to the mailbox that was
just trimmed.

I will also like to have a verbose mode that allows the user when the
script is run to ok each removal.

I am trying to decide if I should do this in Python or VBScript. Any
suggestions?

Thanks.
 
T

Tim Golden

KDawg44 said:
Hi,

I am frustrated with my users who send large files around the office
instead of using the network shares.
[...]

Which means that there are 8 copies of the same file 4MB taking up
space, or a 4MB file turned into a 32MB file."

So, what I would like, is to write a script that parses the exchange
mailbox, and removes all attachments over a certain size (say 500K)
that are attached to messages that are more than 2 weeks old, or that
are in sent items. I would like to write to a log file all the
changes that are made then email that log file to the mailbox that was
just trimmed.

I have something v. similar, only it's at work and I'm not. Maybe tomorrow.
What it does -- I think, it's been a while -- is to rip through any
mailboxes finding attachments over a certain size, saving them to some
kind of folder structure on the user's home drive and replacing the
attachment by a link to the attachment. I have another script which,
independently, rips through users' home shares finding duplicates and
linking them to one copy.

It's a while since they were last run to they're probably quite
dusty but it sounds like the kind of thing you're after.

TJG
 
K

KDawg44

KDawg44 said:
I am frustrated with my users who send large files around the office
instead of using the network shares.
[...]

Which means that there are 8 copies of the same file 4MB taking up
space, or a 4MB file turned into a 32MB file."
So, what I would like, is to write a script that parses the exchange
mailbox, and removes all attachments over a certain size (say 500K)
that are attached to messages that are more than 2 weeks old, or that
are in sent items. I would like to write to a log file all the
changes that are made then email that log file to the mailbox that was
just trimmed.

I have something v. similar, only it's at work and I'm not. Maybe tomorrow.
What it does -- I think, it's been a while -- is to rip through any
mailboxes finding attachments over a certain size, saving them to some
kind of folder structure on the user's home drive and replacing the
attachment by a link to the attachment. I have another script which,
independently, rips through users' home shares finding duplicates and
linking them to one copy.

It's a while since they were last run to they're probably quite
dusty but it sounds like the kind of thing you're after.

TJG

That sounds great!

Thanks!
 
T

Tim Golden

[KDawg44]

[Tim Golden]
Well, here's[1] the first part -- running through folders and replacing
big attachments with links. Now I look, it's designed to be run by
a user on his/her own mailbox rather than by an administrator. Naturally
it's got some stuff which is specific to my setup but nothing, I think,
which isn't easily understood and adapted or removed.

One note is that, as it says in the comments, there doesn't seem to
be a way of identifying large attachments as such, only large messages.
So there may be some corner case where an attachment is stripped out
which is in fact quite small. Obviously you could adapt the
process_attachment function to do something fancier than it is based
on the saved file size etc.

There are no dependencies outside the stdlib except, of course, the
pywin32 package. (Which is, for me, part of the stdlib whenever I
install on Windows). Feel free to email me privately or on-list if
you wish if there's anything which isn't clear etc.

TJG

[1] http://timgolden.me.uk/python/downloads/strip_attachments.py
 
T

Tim Golden

Tim said:
[KDawg44]

[Tim Golden]
Well, here's[1] the first part -- running through folders and replacing
big attachments with links.

.... and here's [1] the second part -- running through folders on disk
and replacing duplicate files with a link to the first one found. For
obvious reasons, be careful with this: it deletes files and replaces
them with hardlinks, but there's no rollback. Nor is it so well
commented as the other, I'm afraid, but I think it is still fairly
clear what's going on.

You call it with a space-separated list of directories to search
(usually one in my own experience) and it walks down those directory
trees looking for files of type INCLUDE_EXTENSIONS except for files
called IGNORE_FILES. Its comparison is very simple: comparing md5
hashes with no optimisations; obviously a lot of work could be done
there if it were an issue.

It ends up with nested dictionaries, the outer one keyed by filename,
the inner one by size and containing a list of directories where
files of that name/size are found. The first in the list is considered
the master and all the others are deleted and replaced by NTFS
hardlinks. Probably safer to hardlink first to an alias, *then*
delete and then rename the alias, but I leave that up to you if
you want to use the script at all.

TJG

[1] http://timgolden.me.uk/python/downloads/find_duplicate_files.py
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top