New IMAP Tool: imap_search
Administrators sometimes have the need to learn whether a particular message has been delivered to a user or to get a list of messages from a certain sender (for example). imap_search makes it easy to do that. For example:
imap_search.pl
james@company.com from=james.smith
Folder Date Subject
=====================================================================
Drafts 01 Dec 2013 09:12:13 Important news!
INBOX 03 Dec 2013 10:20:33 Holiday party
You can search on just about any field in the header or the body for that matter.
List messages after December 1, 2013:
imap_search.pl linda.smith sentsince=01-Dec-2013
List Unseen messages
imap_search.pl joe.jones Unseen
Compound searches:
imap_search.pl from=joe or from=jim
imap_search.pl is used to search a user's mailbox for messages matching a set of search criteria. By default all folders are searched but you can limit the search to a single folder if desired.
For example, let's say you want to find all messages sent by john.lombard:
You can combine filter expressions as well:
OR from=tom from=charles
AND subject=test from=marie
"subject=sales meeting" NOT from=linda
from=Mary subject=notification
subject=password NOT subject=new
sentsince=10-Nov-2013
sentbefore=1-Jan-2001
on=5-Dec-2013
"body=agenda for today"
Note that the SEARCH command is not a very sophisticated command as specified by the IMAP RFCs so you will want to keep your searches fairly simple. If your search expression is not acceptable to IMAP you'll get a syntax error. After some testing you will find what works on your IMAP server (some fields aren't searchable on some servers while they work on others. It seems to be implementation-specific).
In some cases you have to insert 'header' to tell the IMAP server to search the ENVELOPE rather than the content header: header Received=xxxxx.
Administrator mode
It is a pain having to supply a user's password with each search so imap_search supports admin mode where you can give the administrator's username and pwd instead. For example: host/user:admin_user/admin_password
Configuration file
Another way to simplify searching is by setting up an imap_search configuration file with the following lines:
server: imap.myserver.com
admin_user: administrator
admin_pwd: secret
You can put the config file in your local directory, /etc, /usr/bin, or /var/tmp and imap_search will find it. Then you just have to supply the username instead of host/user/password:
./imap_search.pl user from=somebody
Much easier, isn't it?
Single folder search
By default imap_search searches all folders but you may want to narrow the search to save time, for example by searching just the inbox. To limit the search place the name of the folder at the end of the access string like this:
host/user/password:Inbox
Notes:
o Searches are case-insensitive
o Mailbox names are case-sensitive except for the inbox
o Regular expressions are not permitted
o Wildcards are implied, eg subject=test matches every subject which has the string "test" in it
o Search values containing spaces must be quoted: suubject="this is the day"
o "Body" searches take longer since the body of each message has to be searched
o Dates must be in DD-MM-YYYY format without the timestamp or timezone
o See the IMAP RFC for a listing of all IMAP SEARCH keywords or visit:
Imap.Search Method (Boolean, String, String)
-Rick