Introduction To Mailcap File Handling

Introduction To Mailcap File Handling


Mailcap files are used to configure how MIME-aware applications such as mail readers and Web browsers react to files with different MIME types. (The name mailcap is derived from the phrase Cmail capability.) For example, a mailcap file might contain a line like video/mpeg; xmpeg %s. Then, if the user encounters an email message or Web document with the MIME type video/mpeg, %s will be replaced by a filename (usually one belonging to a temporary file) and the xmpeg program can be automatically started to view the file.

The mailcap format is documented in RFC 1524. However, mailcap files are supported on most Unix systems


$ pip install mailcap-fix


findmatch(caps, MIMEtype, key='view', filename='/dev/null', plist=[]):

Find a match for a mailcap entry.

Returns a tuple containing the command line, and the mailcap entry

used; (None, None) if no match is found.  This may invoke the

'test' command of several matching entries before deciding which

entry to use.

getcaps():Returns a dictionary containing the mailcap database.

The dictionary maps a MIME type (in all lowercase, e.g. 'text/plain')

to a list of dictionaries corresponding to mailcap entries.  The list

collects all the entries for that MIME type from all available mailcap

files.  Each dictionary contains key-value pairs for that MIME type,

where the viewing command is stored with the key "view".


import mailcap
d = mailcap.getcaps()
mailcap.findmatch(d, 'video/mpeg', filename='tmp1223')
('xmpeg tmp1223', {'view': 'xmpeg %s'})