mailcap —- Mailcap file handling

Source code:Lib/mailcap.py


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

The mailcap format is documented in RFC 1524, "A User Agent ConfigurationMechanism For Multimedia Mail Format Information," but is not an Internetstandard. However, mailcap files are supported on most Unix systems.

  • mailcap.findmatch(caps, MIMEtype, key='view', filename='/dev/null', plist=[])
  • Return a 2-tuple; the first element is a string containing the command line tobe executed (which can be passed to os.system()), and the second elementis the mailcap entry for a given MIME type. If no matching MIME type can befound, (None, None) is returned.

key is the name of the field desired, which represents the type of activity tobe performed; the default value is 'view', since in the most common case yousimply want to view the body of the MIME-typed data. Other possible valuesmight be 'compose' and 'edit', if you wanted to create a new body of the givenMIME type or alter the existing body data. See RFC 1524 for a complete listof these fields.

filename is the filename to be substituted for %s in the command line; thedefault value is '/dev/null' which is almost certainly not what you want, sousually you'll override it by specifying a filename.

plist can be a list containing named parameters; the default value is simplyan empty list. Each entry in the list must be a string containing the parametername, an equals sign ('='), and the parameter's value. Mailcap entries cancontain named parameters like %{foo}, which will be replaced by the valueof the parameter named 'foo'. For example, if the command line showpartial%{id} %{number} %{total} was in a mailcap file, and plist was set to['id=1', 'number=2', 'total=3'], the resulting command line would be'showpartial 1 2 3'.

In a mailcap file, the "test" field can optionally be specified to test someexternal condition (such as the machine architecture, or the window system inuse) to determine whether or not the mailcap line applies. findmatch()will automatically check such conditions and skip the entry if the check fails.

  • mailcap.getcaps()
  • Returns a dictionary mapping MIME types to a list of mailcap file entries. Thisdictionary must be passed to the findmatch() function. An entry is storedas a list of dictionaries, but it shouldn't be necessary to know the details ofthis representation.

The information is derived from all of the mailcap files found on the system.Settings in the user's mailcap file $HOME/.mailcap will overridesettings in the system mailcap files /etc/mailcap,/usr/etc/mailcap, and /usr/local/etc/mailcap.

An example usage:

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