Searching eMail messages for local users using Dovecot ‘Doveadm-Search’.

Doveadm-Search helps eMail Servers Administrators to show a list of mailbox GUIDs and message UIDs matching given search query.

Synopsis:
doveadm [-Dv] [-f formatter] search [-S socket_path] search_query
doveadm [-Dv] [-f formatter] search [-S socket_path] -A search_query
doveadm [-Dv] [-f formatter] search [-S socket_path] -u user search_query

Description:
The search command is used to find matching messages and, as result, will print the mailbox’s guid and the message’s uid for each match.
When used with the -A or -u wildcard options, it will print the fields username, mailbox-guid and uid for each matching message.

In the first form, doveadm will executed the search action with the environment of the logged in system user. In the second form, the command will be performed for all users. In the third form, only matching mails of the given user(s) will be searched.

Options:
-D
Enables verbosity and debug messages.
-f formatter
Specifies the formatter for formatting the output. Supported formatters are:
flow
prints each line with key=value pairs.
pager
prints each key: value pair on its own line and separates records with form feed character (^L).
tab
prints a table header followed by tab separated value lines.
table
prints a table header followed by adjusted value lines.
-v
Enables verbosity, including progress counter.
-A
If the -A option is present, the command will be performed for all users. Using this option in combination with system users from userdb { driver = passwd } is not recommended, because it contains also users with a lower UID than the one configured with the first_valid_uid setting.
When the SQL userdb module is used make sure that the iterate_query setting in /etc/dovecot/dovecot-sql.conf.ext matches your database layout. When using the LDAP userdb module, make sure that the iterate_attrs and iterate_filter settings in /etc/dovecot/dovecot-ldap.conf.ext match your LDAP schema. Otherwise doveadm will be unable to iterate over all users.
-S socket_path
The option’s argument is either an absolute path to a local UNIX domain socket, or a hostname and port (hostname:port), in order to connect a remote host via a TCP socket.
This allows an administrator to execute doveadm(1) mail commands through the given socket.
-u user/mask
Run the command only for the given user. It’s also possible to use ‘*’ and ‘?’ wildcards (e.g. -u *@example.org).
When neither the -A option nor -u user was specified, the command will be executed with the environment of the currently logged in user.

Arguments:
search_query
Show messages matching this search query. See doveadm-search-query(7) for details.

Search Keys (Overview of search queries for doveadm mailbox commands):
The following search keys from the specification of IMAP version 4 revision 1 (see: RFC 3501, section 6.4.4) are supported:

ALL
Matches all messages.
ANSWERED
Matches messages with the IMAP flag \Answered set.
BCC pattern
Matches messages, which contain pattern in the BCC field of the message’s IMAP envelope structure.
BEFORE date specification
Matches messages with an internal date before date specification.
BODY pattern
Matches messages, which contain pattern in the body part.
CC pattern
Matches messages, which contain pattern in the CC field of the message’s IMAP envelope structure.
DELETED
Matches messages with the IMAP flag \Deleted set.
DRAFT
Matches messages with the IMAP flag \Draft set.
FLAGGED
Matches messages with the IMAP flag \Flagged set.
FROM pattern
Matches messages, which contain pattern in the FROM field of the message’s IMAP envelope structure.
HEADER field pattern
Matches messages, which either have the named header field, when empty pattern was given. Or messages, where the given header field’s value contains the specified pattern.
KEYWORD keyword
Matches messages with the given IMAP keyword (e.g. $Forwarded) flag set.
LARGER size
Matches messages that are larger than the specified size.
MAILBOX name
Matches messages in the mailbox with the specified name.
MAILBOX-GUID guid
Matches messages in the mailbox with the specified guid.
NEW
Matches messages, which have the IMAP flag \Recent set but not the IMAP flag \Seen.
NOT search key
Inverse matching – matches massages, where the search doesn’t match the specified search key or its value.
OLD
Matches messages, which do not have the IMAP flag \Recent set.
ON date specification
Matches messages whose internal date matches the given date specification.
search key OR search key
Matches messages where one of the OR-ed search keys matches.
Note: IMAP4rev1 uses the syntax: OR search key search key
RECENT
Matches messages with the IMAP flag \Recent set.
SEEN
Matches messages with the IMAP flag \Seen set.
SENTBEFORE date specification
Matches messages with a Date: header before date specification.
SENTON date specification
Matches messages with a Date: header matching the given date specification.
SENTSINCE date specification
Matches messages with a Date: header matching or after the given date specification.
SINCE date specification
Matches messages whose internal date is within or after the given date specification.
SMALLER size
Matches messages with a size smaller than the given size.
SUBJECT pattern
Matches messages, which contain pattern in the SUBJECT field of the message’s IMAP envelope structure.
TEXT pattern
Matches messages, which contain pattern in the message body.
TO pattern
Matches messages, which contain pattern in the TO field of the message’s IMAP envelope structure.
UID sequence
Matches messages with the given UID(s). A sequence may be a single UID. Can be a sequence range, written as from:to, e.g. 100:125. As comma separated list of UIDs, e.g. 11,50,4. It’s also possible to combine multiple sequences, e.g. 1,3,5,7,10:20.
UNANSWERED
Matches messages, which do not have the IMAP flag \Answered set.
UNDELETED
Matches messages, which do not have the IMAP flag \Deleted set.
UNDRAFT
Matches messages, which do not have the IMAP flag \Draft set.
UNFLAGGED
Matches messages, which do not have the IMAP flag \Flagged set.
UNKEYWORD keyword
Matches messages, which do not have the given IMAP keyword flag set
UNSEEN
Matches messages, which do not have the IMAP flag \Seen set.

Additional search keys, provided by doveadm:
SAVEDBEFORE date specification
Matches messages, which were saved before date specification.
SAVEDON date specification
Matches messages whose save date matches the given date specification.
SAVEDSINCE date specification
Matches messages with a save date matching or after the given date specification.

Date Specification:
doveadm supports a few additional date specification formats. They can be used anywhere, where a date specification value is obligatory.

day-month-year
Default IMAP4rev1 date format.
day, the day of month: 1-31.
month, the abbreviated month name: Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov or Dec.
year, four digits of year, e.g. 2007.
For example the “13th of April 2007” will be represented as 13-Apr-2007.
interval
Combination of a positive integer number and a time unit.
Available time units are: weeks (abbr: w), days (abbr: d), hours (abbr: h), mins (abbr: m) and secs (abbr: s).
To match messages from last week, you may specify for example: since 1w, since 1weeks or since 7days.
Unix timestamp
A 10 digit Unix timestamp, seconds since the 1st of January 1970, 00:00:00 UTC. For example the “13th of April 2007” will be represented as 1176418800.
YYYY-MM-DD
Extended ISO-8601 calendar date format. For example the “13th of April 2007” will be represented as 2007-04-13.

Size:
doveadm provides also an additional size representation format. The following formats can be used anywhere, where a size value is obligatory.

octets
The message size in octets, as specified in the IMAP4rev1 specification.
size
The message size in B (byte), k (kilobyte), M (megabyte), G (gigabyte) or T (terabyte).
To match messages, bigger than 1 megabyte, you may specify for example: larger 1M or larger 1024k.

Examples:
This example demonstrates how to search in user JohnDoe’s dovecot mailboxes all messages, which contains the word “example” in the Subject: header.

doveadm search -u JohnDoe mailbox dovecot\* subject example
3a94c928d66ebe4bda04000015811c6a        8
3a94c928d66ebe4bda04000015811c6a        25
3a94c928d66ebe4bda04000015811c6a        45

The search command is mainly useful when used together with doveadm fetch command. For example to save JohnDoe’s message bodies of all messages from INBOX that have “example” in subject, use:

doveadm search -u JohnDoe mailbox INBOX subject todo |
while read guid uid; do
  doveadm fetch -u JohnDoe body \
          mailbox-guid $guid uid $uid > msg.$uid
done

To search all messages older than two weeks (using SAVEDBEFORE:

doveadm search -A savedbefore 2w

Comments are closed.