3 @setfilename emacs-mime
4 @settitle Emacs MIME Manual
10 * Emacs MIME: (emacs-mime). The MIME de/composition library.
15 @setchapternewpage odd
19 This file documents the Emacs MIME interface functionality.
21 Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
23 Permission is granted to copy, distribute and/or modify this document
24 under the terms of the GNU Free Documentation License, Version 1.1 or
25 any later version published by the Free Software Foundation; with no
26 Invariant Sections, with the Front-Cover texts being ``A GNU
27 Manual'', and with the Back-Cover Texts as in (a) below. A copy of the
28 license is included in the section entitled ``GNU Free Documentation
29 License'' in the Emacs manual.
31 (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify
32 this GNU Manual, like GNU software. Copies published by the Free
33 Software Foundation raise funds for GNU development.''
35 This document is part of a collection distributed under the GNU Free
36 Documentation License. If you want to distribute this document
37 separately from the collection, you can do so by adding a copy of the
38 license to the document, as described in section 6 of the license.
44 @title Emacs MIME Manual
46 @author by Lars Magne Ingebrigtsen
49 @vskip 0pt plus 1filll
50 Copyright @copyright{} 1998, 1999, 2000, 2001, 2002 Free Software
53 Permission is granted to copy, distribute and/or modify this document
54 under the terms of the GNU Free Documentation License, Version 1.1 or
55 any later version published by the Free Software Foundation; with the
56 Invariant Sections being none, with the Front-Cover texts being ``A GNU
57 Manual'', and with the Back-Cover Texts as in (a) below. A copy of the
58 license is included in the section entitled ``GNU Free Documentation
59 License'' in the Emacs manual.
61 (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify
62 this GNU Manual, like GNU software. Copies published by the Free
63 Software Foundation raise funds for GNU development.''
65 This document is part of a collection distributed under the GNU Free
66 Documentation License. If you want to distribute this document
67 separately from the collection, you can do so by adding a copy of the
68 license to the document, as described in section 6 of the license.
77 This manual documents the libraries used to compose and display
80 This manual is directed at users who want to modify the behaviour of
81 the @sc{mime} encoding/decoding process or want a more detailed
82 picture of how the Emacs @sc{mime} library works, and people who want
83 to write functions and commands that manipulate @sc{mime} elements.
85 @sc{mime} is short for @dfn{Multipurpose Internet Mail Extensions}.
86 This standard is documented in a number of RFCs; mainly RFC2045 (Format
87 of Internet Message Bodies), RFC2046 (Media Types), RFC2047 (Message
88 Header Extensions for Non-ASCII Text), RFC2048 (Registration
89 Procedures), RFC2049 (Conformance Criteria and Examples). It is highly
90 recommended that anyone who intends writing @sc{mime}-compliant software
91 read at least RFC2045 and RFC2047.
94 * Decoding and Viewing:: A framework for decoding and viewing.
95 * Composing:: MML; a language for describing @sc{mime} parts.
96 * Interface Functions:: An abstraction over the basic functions.
97 * Basic Functions:: Utility and basic parsing functions.
98 * Standards:: A summary of RFCs and working documents used.
99 * Index:: Function and variable index.
103 @node Decoding and Viewing
104 @chapter Decoding and Viewing
106 This chapter deals with decoding and viewing @sc{mime} messages on a
109 The main idea is to first analyze a @sc{mime} article, and then allow
110 other programs to do things based on the list of @dfn{handles} that are
111 returned as a result of this analysis.
114 * Dissection:: Analyzing a @sc{mime} message.
115 * Non-MIME:: Analyzing a non-@sc{mime} message.
116 * Handles:: Handle manipulations.
117 * Display:: Displaying handles.
118 * Display Customization:: Variables that affect display.
119 * New Viewers:: How to write your own viewers.
126 The @code{mm-dissect-buffer} is the function responsible for dissecting
127 a @sc{mime} article. If given a multipart message, it will recursively
128 descend the message, following the structure, and return a tree of
129 @sc{mime} handles that describes the structure of the message.
133 @vindex mm-uu-configure-list
135 Gnus also understands some non-@sc{mime} attachments, such as
136 postscript, uuencode, binhex, yenc, shar, forward, gnatsweb, pgp.
137 Each of these features can be disabled by add an item into
138 @code{mm-uu-configure-list}. For example,
142 (add-to-list 'mm-uu-configure-list '(pgp-signed . disabled))
168 Non-@sc{mime} forwarded message.
176 PGP signed clear text.
179 @findex pgp-encrypted
180 PGP encrypted clear text.
187 @findex emacs-sources
188 Emacs source code. This item works only in the groups matching
189 @code{mm-uu-emacs-sources-regexp}.
196 A @sc{mime} handle is a list that fully describes a @sc{mime}
199 The following macros can be used to access elements in a handle:
202 @item mm-handle-buffer
203 @findex mm-handle-buffer
204 Return the buffer that holds the contents of the undecoded @sc{mime}
208 @findex mm-handle-type
209 Return the parsed @code{Content-Type} of the part.
211 @item mm-handle-encoding
212 @findex mm-handle-encoding
213 Return the @code{Content-Transfer-Encoding} of the part.
215 @item mm-handle-undisplayer
216 @findex mm-handle-undisplayer
217 Return the object that can be used to remove the displayed part (if it
220 @item mm-handle-set-undisplayer
221 @findex mm-handle-set-undisplayer
222 Set the undisplayer object.
224 @item mm-handle-disposition
225 @findex mm-handle-disposition
226 Return the parsed @code{Content-Disposition} of the part.
228 @item mm-handle-disposition
229 @findex mm-handle-disposition
230 Return the description of the part.
232 @item mm-get-content-id
233 Returns the handle(s) referred to by @code{Content-ID}.
241 Functions for displaying, removing and saving.
244 @item mm-display-part
245 @findex mm-display-part
249 @findex mm-remove-part
250 Remove the part (if it has been displayed).
253 @findex mm-inlinable-p
254 Say whether a @sc{mime} type can be displayed inline.
256 @item mm-automatic-display-p
257 @findex mm-automatic-display-p
258 Say whether a @sc{mime} type should be displayed automatically.
260 @item mm-destroy-part
261 @findex mm-destroy-part
262 Free all resources occupied by a part.
266 Offer to save the part in a file.
270 Offer to pipe the part to some process.
272 @item mm-interactively-view-part
273 @findex mm-interactively-view-part
274 Prompt for a mailcap method to use to view the part.
279 @node Display Customization
280 @section Display Customization
284 @item mm-inline-media-tests
285 @vindex mm-inline-media-tests
286 This is an alist where the key is a @sc{mime} type, the second element
287 is a function to display the part @dfn{inline} (i.e., inside Emacs), and
288 the third element is a form to be @code{eval}ed to say whether the part
289 can be displayed inline.
291 This variable specifies whether a part @emph{can} be displayed inline,
292 and, if so, how to do it. It does not say whether parts are
293 @emph{actually} displayed inline.
295 @item mm-inlined-types
296 @vindex mm-inlined-types
297 This, on the other hand, says what types are to be displayed inline, if
298 they satisfy the conditions set by the variable above. It's a list of
299 @sc{mime} media types.
301 @item mm-automatic-display
302 @vindex mm-automatic-display
303 This is a list of types that are to be displayed ``automatically'', but
304 only if the above variable allows it. That is, only inlinable parts can
305 be displayed automatically.
307 @item mm-automatic-external-display
308 @vindex mm-automatic-external-display
309 This is a list of types that will be displayed automatically in an
312 @item mm-attachment-override-types
313 @vindex mm-attachment-override-types
314 Some @sc{mime} agents create parts that have a content-disposition of
315 @samp{attachment}. This variable allows overriding that disposition and
316 displaying the part inline. (Note that the disposition is only
317 overridden if we are able to, and want to, display the part inline.)
319 @item mm-discouraged-alternatives
320 @vindex mm-discouraged-alternatives
321 List of @sc{mime} types that are discouraged when viewing
322 @samp{multipart/alternative}. Viewing agents are supposed to view the
323 last possible part of a message, as that is supposed to be the richest.
324 However, users may prefer other types instead, and this list says what
325 types are most unwanted. If, for instance, @samp{text/html} parts are
326 very unwanted, and @samp{text/richtext} parts are somewhat unwanted,
327 you could say something like:
330 (setq mm-discouraged-alternatives
331 '("text/html" "text/richtext")
333 (remove "text/html" mm-automatic-display))
336 @item mm-inline-large-images
337 @vindex mm-inline-large-images
338 When displaying inline images that are larger than the window, XEmacs
339 does not enable scrolling, which means that you cannot see the whole
340 image. To prevent this, the library tries to determine the image size
341 before displaying it inline, and if it doesn't fit the window, the
342 library will display it externally (e.g. with @samp{ImageMagick} or
343 @samp{xv}). Setting this variable to @code{t} disables this check and
344 makes the library display all inline images as inline, regardless of
347 @item mm-inline-override-types
348 @vindex mm-inline-override-types
349 @code{mm-inlined-types} may include regular expressions, for example to
350 specify that all @samp{text/.*} parts be displayed inline. If a user
351 prefers to have a type that matches such a regular expression be treated
352 as an attachment, that can be accomplished by setting this variable to a
353 list containing that type. For example assuming @code{mm-inlined-types}
354 includes @samp{text/.*}, then including @samp{text/html} in this
355 variable will cause @samp{text/html} parts to be treated as attachments.
357 @item mm-text-html-renderer
358 @vindex mm-text-html-renderer
359 This selects the function used to render @sc{html}. The predefined
360 renderers are selected by the symbols @code{w3},
361 @code{w3m}@footnote{See @uref{http://emacs-w3m.namazu.org/} for more
362 information about emacs-w3m}, @code{links}, @code{lynx},
363 @code{w3m-standalone} or @code{html2text}. If @code{nil} use an
364 external viewer. You can also specify a function, which will be
365 called with a @sc{mime} handle as the argument.
367 @item mm-inline-text-html-with-images
368 @vindex mm-inline-text-html-with-images
369 Some @sc{html} mails might have the trick of spammers using
370 @samp{<img>} tags. It is likely to be intended to verify whether you
371 have read the mail. You can prevent your personal informations from
372 leaking by setting this option to @code{nil} (which is the default).
373 It is currently ignored by Emacs/w3. For emacs-w3m, you may use the
374 command @kbd{t} on the image anchor to show an image even if it is
375 @code{nil}.@footnote{The command @kbd{T} will load all images. If you
376 have set the option @code{w3m-key-binding} to @code{info}, use @kbd{i}
379 @item mm-w3m-safe-url-regexp
380 @vindex mm-w3m-safe-url-regexp
381 A regular expression that matches safe URL names, i.e. URLs that are
382 unlikely to leak personal information when rendering @sc{html} email
383 (the default value is @samp{\\`cid:}). If @code{nil} consider all
386 @item mm-inline-text-html-with-w3m-keymap
387 @vindex mm-inline-text-html-with-w3m-keymap
388 You can use emacs-w3m command keys in the inlined text/html part by
389 setting this option to non-@code{nil}. The default value is @code{t}.
391 @item mm-external-terminal-program
392 @vindex mm-external-terminal-program
393 The program used to start an external terminal.
401 Here's an example viewer for displaying @code{text/enriched} inline:
404 (defun mm-display-enriched-inline (handle)
407 (mm-insert-part handle)
408 (save-window-excursion
409 (enriched-decode (point-min) (point-max))
410 (setq text (buffer-string))))
411 (mm-insert-inline handle text)))
414 We see that the function takes a @sc{mime} handle as its parameter. It
415 then goes to a temporary buffer, inserts the text of the part, does some
416 work on the text, stores the result, goes back to the buffer it was
417 called from and inserts the result.
419 The two important helper functions here are @code{mm-insert-part} and
420 @code{mm-insert-inline}. The first function inserts the text of the
421 handle in the current buffer. It handles charset and/or content
422 transfer decoding. The second function just inserts whatever text you
423 tell it to insert, but it also sets things up so that the text can be
424 ``undisplayed' in a convenient manner.
430 @cindex MIME Composing
432 @cindex MIME Meta Language
434 Creating a @sc{mime} message is boring and non-trivial. Therefore, a
435 library called @code{mml} has been defined that parses a language called
436 MML (@sc{mime} Meta Language) and generates @sc{mime} messages.
438 @findex mml-generate-mime
439 The main interface function is @code{mml-generate-mime}. It will
440 examine the contents of the current (narrowed-to) buffer and return a
441 string containing the @sc{mime} message.
444 * Simple MML Example:: An example MML document.
445 * MML Definition:: All valid MML elements.
446 * Advanced MML Example:: Another example MML document.
447 * Encoding Customization:: Variables that affect encoding.
448 * Charset Translation:: How charsets are mapped from @sc{mule} to @sc{mime}.
449 * Conversion:: Going from @sc{mime} to MML and vice versa.
450 * Flowed text:: Soft and hard newlines.
454 @node Simple MML Example
455 @section Simple MML Example
457 Here's a simple @samp{multipart/alternative}:
460 <#multipart type=alternative>
461 This is a plain text part.
462 <#part type=text/enriched>
463 <center>This is a centered enriched part</center>
467 After running this through @code{mml-generate-mime}, we get this:
470 Content-Type: multipart/alternative; boundary="=-=-="
476 This is a plain text part.
479 Content-Type: text/enriched
482 <center>This is a centered enriched part</center>
489 @section MML Definition
491 The MML language is very simple. It looks a bit like an SGML
492 application, but it's not.
494 The main concept of MML is the @dfn{part}. Each part can be of a
495 different type or use a different charset. The way to delineate a part
496 is with a @samp{<#part ...>} tag. Multipart parts can be introduced
497 with the @samp{<#multipart ...>} tag. Parts are ended by the
498 @samp{<#/part>} or @samp{<#/multipart>} tags. Parts started with the
499 @samp{<#part ...>} tags are also closed by the next open tag.
501 There's also the @samp{<#external ...>} tag. These introduce
502 @samp{external/message-body} parts.
504 Each tag can contain zero or more parameters on the form
505 @samp{parameter=value}. The values may be enclosed in quotation marks,
506 but that's not necessary unless the value contains white space. So
507 @samp{filename=/home/user/#hello$^yes} is perfectly valid.
509 The following parameters have meaning in MML; parameters that have no
510 meaning are ignored. The MML parameter names are the same as the
511 @sc{mime} parameter names; the things in the parentheses say which
512 header it will be used in.
516 The @sc{mime} type of the part (@code{Content-Type}).
519 Use the contents of the file in the body of the part
520 (@code{Content-Disposition}).
523 The contents of the body of the part are to be encoded in the character
524 set speficied (@code{Content-Type}). @xref{Charset Translation}.
527 Might be used to suggest a file name if the part is to be saved
528 to a file (@code{Content-Type}).
531 Valid values are @samp{inline} and @samp{attachment}
532 (@code{Content-Disposition}).
535 Valid values are @samp{7bit}, @samp{8bit}, @samp{quoted-printable} and
536 @samp{base64} (@code{Content-Transfer-Encoding}). @xref{Charset
540 A description of the part (@code{Content-Description}).
543 RFC822 date when the part was created (@code{Content-Disposition}).
545 @item modification-date
546 RFC822 date when the part was modified (@code{Content-Disposition}).
549 RFC822 date when the part was read (@code{Content-Disposition}).
552 Who to encrypt/sign the part to. This field is used to override any
553 auto-detection based on the To/CC headers.
556 The size (in octets) of the part (@code{Content-Disposition}).
559 What technology to sign this MML part with (@code{smime}, @code{pgp}
563 What technology to encrypt this MML part with (@code{smime},
564 @code{pgp} or @code{pgpmime})
568 Parameters for @samp{application/octet-stream}:
572 Type of the part; informal---meant for human readers
573 (@code{Content-Type}).
576 Parameters for @samp{message/external-body}:
580 A word indicating the supported access mechanism by which the file may
581 be obtained. Values include @samp{ftp}, @samp{anon-ftp}, @samp{tftp},
582 @samp{localfile}, and @samp{mailserver}. (@code{Content-Type}.)
585 The RFC822 date after which the file may no longer be fetched.
586 (@code{Content-Type}.)
589 The size (in octets) of the file. (@code{Content-Type}.)
592 Valid values are @samp{read} and @samp{read-write}
593 (@code{Content-Type}).
597 Parameters for @samp{sign=smime}:
602 File containing key and certificate for signer.
606 Parameters for @samp{encrypt=smime}:
611 File containing certificate for recipient.
616 @node Advanced MML Example
617 @section Advanced MML Example
619 Here's a complex multipart message. It's a @samp{multipart/mixed} that
620 contains many parts, one of which is a @samp{multipart/alternative}.
623 <#multipart type=mixed>
624 <#part type=image/jpeg filename=~/rms.jpg disposition=inline>
625 <#multipart type=alternative>
626 This is a plain text part.
627 <#part type=text/enriched name=enriched.txt>
628 <center>This is a centered enriched part</center>
630 This is a new plain text part.
631 <#part disposition=attachment>
632 This plain text part is an attachment.
636 And this is the resulting @sc{mime} message:
639 Content-Type: multipart/mixed; boundary="=-=-="
647 Content-Type: image/jpeg;
649 Content-Disposition: inline;
651 Content-Transfer-Encoding: base64
653 /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRof
654 Hh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/wAALCAAwADABAREA/8QAHwAA
655 AQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQR
656 BRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RF
657 RkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ip
658 qrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/9oACAEB
659 AAA/AO/rifFHjldNuGsrDa0qcSSHkA+gHrXKw+LtWLrMb+RgTyhbr+HSug07xNqV9fQtZrNI
660 AyiaE/NuBPOOOP0rvRNE880KOC8TbXXGCv1FPqjrF4LDR7u5L7SkTFT/ALWOP1xXgTuXfc7E
661 sx6nua6rwp4IvvEM8chCxWxOdzn7wz6V9AaB4S07w9p5itow0rDLSY5Pt9K43xO66P4xs71m
662 2QXiGCbA4yOVJ9+1aYORkdK434lyNH4ahCnG66VT9Nj15JFbPdX0MS43M4VQf5/yr2vSpLnw
663 5ZW8dlCZ8KFXjOPX0/mK6rSPEGt3Angu44fNEReHYNvIH3TzXDeKNO8RX+kSX2ouZkicTIOc
664 L+g7E810ulFjpVtv3bwgB3HJyK5L4quY/C9sVxk3ij/xx6850u7t1mtp/wDlpEw3An3Jr3Dw
665 34gsbWza4nBlhC5LDsaW6+IFgupQyCF3iHH7gA7c9R9ay7zx6t7aX9jHC4smhfBkGCvHGfrm
666 tLQ7hbnRrV1GPkAP1x1/Hr+Ncr8Vzjwrbf8AX6v/AKA9eQRyYlQk8Yx9K6XTNbkgia2ciSIn
667 7p5Ga9Atte0LTLKO6it4i7dVRFJDcZ4PvXN+JvEMF9bILVGXJLSZ4zkjivRPDaeX4b08HOTC
668 pOffmua+KkbS+GLVUGT9tT/0B68eeIpIFYjB70+OOVXyoOM9+M1eaWeCLzHPyHGO/NVWvJJm
669 jQ8KGH1NfQWhXSXmh2c8eArRLwO3HSv/2Q==
672 Content-Type: multipart/alternative; boundary="==-=-="
678 This is a plain text part.
681 Content-Type: text/enriched;
685 <center>This is a centered enriched part</center>
691 This is a new plain text part.
694 Content-Disposition: attachment
697 This plain text part is an attachment.
702 @node Encoding Customization
703 @section Encoding Customization
707 @item mm-body-charset-encoding-alist
708 @vindex mm-body-charset-encoding-alist
709 Mapping from @sc{mime} charset to encoding to use. This variable is
710 usually used except, e.g., when other requirements force a specific
711 encoding (digitally signed messages require 7bit encodings). The
712 default is @code{((iso-2022-jp . 7bit) (iso-2022-jp-2 . 7bit))}. As
713 an example, if you do not want to have ISO-8859-1 characters
714 quoted-printable encoded, you may add @code{(iso-8859-1 . 8bit)} to
715 this variable. You can override this setting on a per-message basis
716 by using the @code{encoding} MML tag (@pxref{MML Definition}).
718 @item mm-coding-system-priorities
719 @vindex mm-coding-system-priorities
720 Prioritize coding systems to use for outgoing messages. The default
721 is nil, which means to use the defaults in Emacs. It is a list of
722 coding system symbols (aliases of coding systems does not work, use
723 @kbd{M-x describe-coding-system} to make sure you are not specifying
724 an alias in this variable). For example, if you have configured Emacs
725 to use prefer UTF-8, but wish that outgoing messages should be sent in
726 ISO-8859-1 if possible, you can set this variable to
727 @code{(iso-latin-1)}. You can override this setting on a per-message
728 basis by using the @code{charset} MML tag (@pxref{MML Definition}).
730 @item mm-content-transfer-encoding-defaults
731 @vindex mm-content-transfer-encoding-defaults
732 Mapping from @sc{mime} types to encoding to use. This variable is usually
733 used except, e.g., when other requirements force a safer encoding
734 (digitally signed messages require 7bit encoding). Besides the normal
735 @sc{mime} encodings, @code{qp-or-base64} may be used to indicate that for
736 each case the most efficient of quoted-printable and base64 should be
737 used. You can override this setting on a per-message basis by using
738 the @code{encoding} MML tag (@pxref{MML Definition}).
740 @item mm-use-ultra-safe-encoding
741 @vindex mm-use-ultra-safe-encoding
742 When this is non-nil, it means that textual parts are encoded as
743 quoted-printable if they contain lines longer than 76 characters or
744 starting with "From " in the body. Non-7bit encodings (8bit, binary)
745 are generally disallowed. This reduce the probability that a non-8bit
746 clean MTA or MDA changes the message. This should never be set
747 directly, but bound by other functions when necessary (e.g., when
748 encoding messages that are to be digitally signed).
752 @node Charset Translation
753 @section Charset Translation
756 During translation from MML to @sc{mime}, for each @sc{mime} part which
757 has been composed inside Emacs, an appropriate charset has to be chosen.
759 @vindex mail-parse-charset
760 If you are running a non-@sc{mule} Emacs, this process is simple: If the
761 part contains any non-ASCII (8-bit) characters, the @sc{mime} charset
762 given by @code{mail-parse-charset} (a symbol) is used. (Never set this
763 variable directly, though. If you want to change the default charset,
764 please consult the documentation of the package which you use to process
766 @xref{Various Message Variables, , Various Message Variables, message,
767 Message Manual}, for example.)
768 If there are only ASCII characters, the @sc{mime} charset US-ASCII is
774 @vindex mm-mime-mule-charset-alist
775 Things are slightly more complicated when running Emacs with @sc{mule}
776 support. In this case, a list of the @sc{mule} charsets used in the
777 part is obtained, and the @sc{mule} charsets are translated to @sc{mime}
778 charsets by consulting the variable @code{mm-mime-mule-charset-alist}.
779 If this results in a single @sc{mime} charset, this is used to encode
780 the part. But if the resulting list of @sc{mime} charsets contains more
781 than one element, two things can happen: If it is possible to encode the
782 part via UTF-8, this charset is used. (For this, Emacs must support
783 the @code{utf-8} coding system, and the part must consist entirely of
784 characters which have Unicode counterparts.) If UTF-8 is not available
785 for some reason, the part is split into several ones, so that each one
786 can be encoded with a single @sc{mime} charset. The part can only be
787 split at line boundaries, though---if more than one @sc{mime} charset is
788 required to encode a single line, it is not possible to encode the part.
790 When running Emacs with @sc{mule} support, the preferences for which
791 coding system to use is inherited from Emacs itself. This means that
792 if Emacs is set up to prefer UTF-8, it will be used when encoding
793 messages. You can modify this by altering the
794 @code{mm-coding-system-priorities} variable though (@pxref{Encoding
797 The charset to be used can be overriden by setting the @code{charset}
798 MML tag (@pxref{MML Definition}) when composing the message.
800 The encoding of characters (quoted-printable, 8bit etc) is orthogonal
801 to the discussion here, and is controlled by the variables
802 @code{mm-body-charset-encoding-alist} and
803 @code{mm-content-transfer-encoding-defaults} (@pxref{Encoding
810 A (multipart) @sc{mime} message can be converted to MML with the
811 @code{mime-to-mml} function. It works on the message in the current
812 buffer, and substitutes MML markup for @sc{mime} boundaries.
813 Non-textual parts do not have their contents in the buffer, but instead
814 have the contents in separate buffers that are referred to from the MML
818 An MML message can be converted back to @sc{mime} by the
819 @code{mml-to-mime} function.
821 These functions are in certain senses ``lossy''---you will not get back
822 an identical message if you run @sc{mime-to-mml} and then
823 @sc{mml-to-mime}. Not only will trivial things like the order of the
824 headers differ, but the contents of the headers may also be different.
825 For instance, the original message may use base64 encoding on text,
826 while @sc{mml-to-mime} may decide to use quoted-printable encoding, and
829 In essence, however, these two functions should be the inverse of each
830 other. The resulting contents of the message should remain equivalent,
836 @cindex format=flowed
838 The Emacs @sc{mime} library will respect the @code{use-hard-newlines}
839 variable (@pxref{Hard and Soft Newlines, ,Hard and Soft Newlines,
840 emacs, Emacs Manual}) when encoding a message, and the
841 ``format=flowed'' Content-Type parameter when decoding a message.
843 On encoding text, lines terminated by soft newline characters are
844 filled together and wrapped after the column decided by
845 @code{fill-flowed-encode-column}. This variable controls how the text
846 will look in a client that does not support flowed text, the default
847 is to wrap after 66 characters. If hard newline characters are not
848 present in the buffer, no flow encoding occurs.
850 On decoding flowed text, lines with soft newline characters are filled
851 together and wrapped after the column decided by
852 @code{fill-flowed-display-column}. The default is to wrap after
858 @node Interface Functions
859 @chapter Interface Functions
860 @cindex interface functions
863 The @code{mail-parse} library is an abstraction over the actual
864 low-level libraries that are described in the next chapter.
866 Standards change, and so programs have to change to fit in the new
867 mold. For instance, RFC2045 describes a syntax for the
868 @code{Content-Type} header that only allows ASCII characters in the
869 parameter list. RFC2231 expands on RFC2045 syntax to provide a scheme
870 for continuation headers and non-ASCII characters.
872 The traditional way to deal with this is just to update the library
873 functions to parse the new syntax. However, this is sometimes the wrong
874 thing to do. In some instances it may be vital to be able to understand
875 both the old syntax as well as the new syntax, and if there is only one
876 library, one must choose between the old version of the library and the
877 new version of the library.
879 The Emacs @sc{mime} library takes a different tack. It defines a
880 series of low-level libraries (@file{rfc2047.el}, @file{rfc2231.el}
881 and so on) that parses strictly according to the corresponding
882 standard. However, normal programs would not use the functions
883 provided by these libraries directly, but instead use the functions
884 provided by the @code{mail-parse} library. The functions in this
885 library are just aliases to the corresponding functions in the latest
886 low-level libraries. Using this scheme, programs get a consistent
887 interface they can use, and library developers are free to create
888 write code that handles new standards.
890 The following functions are defined by this library:
893 @item mail-header-parse-content-type
894 @findex mail-header-parse-content-type
895 Parse a @code{Content-Type} header and return a list on the following
900 (attribute1 . value1)
901 (attribute2 . value2)
908 (mail-header-parse-content-type
909 "image/gif; name=\"b980912.gif\"")
910 @result{} ("image/gif" (name . "b980912.gif"))
913 @item mail-header-parse-content-disposition
914 @findex mail-header-parse-content-disposition
915 Parse a @code{Content-Disposition} header and return a list on the same
916 format as the function above.
918 @item mail-content-type-get
919 @findex mail-content-type-get
920 Takes two parameters---a list on the format above, and an attribute.
921 Returns the value of the attribute.
924 (mail-content-type-get
925 '("image/gif" (name . "b980912.gif")) 'name)
926 @result{} "b980912.gif"
929 @item mail-header-encode-parameter
930 @findex mail-header-encode-parameter
931 Takes a parameter string and returns an encoded version of the string.
932 This is used for parameters in headers like @code{Content-Type} and
933 @code{Content-Disposition}.
935 @item mail-header-remove-comments
936 @findex mail-header-remove-comments
937 Return a comment-free version of a header.
940 (mail-header-remove-comments
941 "Gnus/5.070027 (Pterodactyl Gnus v0.27) (Finnish Landrace)")
942 @result{} "Gnus/5.070027 "
945 @item mail-header-remove-whitespace
946 @findex mail-header-remove-whitespace
947 Remove linear white space from a header. Space inside quoted strings
948 and comments is preserved.
951 (mail-header-remove-whitespace
952 "image/gif; name=\"Name with spaces\"")
953 @result{} "image/gif;name=\"Name with spaces\""
956 @item mail-header-get-comment
957 @findex mail-header-get-comment
958 Return the last comment in a header.
961 (mail-header-get-comment
962 "Gnus/5.070027 (Pterodactyl Gnus v0.27) (Finnish Landrace)")
963 @result{} "Finnish Landrace"
966 @item mail-header-parse-address
967 @findex mail-header-parse-address
968 Parse an address and return a list containing the mailbox and the
972 (mail-header-parse-address
973 "Hrvoje Niksic <hniksic@@srce.hr>")
974 @result{} ("hniksic@@srce.hr" . "Hrvoje Niksic")
977 @item mail-header-parse-addresses
978 @findex mail-header-parse-addresses
979 Parse a string with list of addresses and return a list of elements like
980 the one described above.
983 (mail-header-parse-addresses
984 "Hrvoje Niksic <hniksic@@srce.hr>, Steinar Bang <sb@@metis.no>")
985 @result{} (("hniksic@@srce.hr" . "Hrvoje Niksic")
986 ("sb@@metis.no" . "Steinar Bang"))
989 @item mail-header-parse-date
990 @findex mail-header-parse-date
991 Parse a date string and return an Emacs time structure.
993 @item mail-narrow-to-head
994 @findex mail-narrow-to-head
995 Narrow the buffer to the header section of the buffer. Point is placed
996 at the beginning of the narrowed buffer.
998 @item mail-header-narrow-to-field
999 @findex mail-header-narrow-to-field
1000 Narrow the buffer to the header under point. Understands continuation
1003 @item mail-header-fold-field
1004 @findex mail-header-fold-field
1005 Fold the header under point.
1007 @item mail-header-unfold-field
1008 @findex mail-header-unfold-field
1009 Unfold the header under point.
1011 @item mail-header-field-value
1012 @findex mail-header-field-value
1013 Return the value of the field under point.
1015 @item mail-encode-encoded-word-region
1016 @findex mail-encode-encoded-word-region
1017 Encode the non-ASCII words in the region. For instance,
1018 @samp{Naïve} is encoded as @samp{=?iso-8859-1?q?Na=EFve?=}.
1020 @item mail-encode-encoded-word-buffer
1021 @findex mail-encode-encoded-word-buffer
1022 Encode the non-ASCII words in the current buffer. This function is
1023 meant to be called narrowed to the headers of a message.
1025 @item mail-encode-encoded-word-string
1026 @findex mail-encode-encoded-word-string
1027 Encode the words that need encoding in a string, and return the result.
1030 (mail-encode-encoded-word-string
1031 "This is naïve, baby")
1032 @result{} "This is =?iso-8859-1?q?na=EFve,?= baby"
1035 @item mail-decode-encoded-word-region
1036 @findex mail-decode-encoded-word-region
1037 Decode the encoded words in the region.
1039 @item mail-decode-encoded-word-string
1040 @findex mail-decode-encoded-word-string
1041 Decode the encoded words in the string and return the result.
1044 (mail-decode-encoded-word-string
1045 "This is =?iso-8859-1?q?na=EFve,?= baby")
1046 @result{} "This is naïve, baby"
1051 Currently, @code{mail-parse} is an abstraction over @code{ietf-drums},
1052 @code{rfc2047}, @code{rfc2045} and @code{rfc2231}. These are documented
1053 in the subsequent sections.
1057 @node Basic Functions
1058 @chapter Basic Functions
1060 This chapter describes the basic, ground-level functions for parsing and
1061 handling. Covered here is parsing @code{From} lines, removing comments
1062 from header lines, decoding encoded words, parsing date headers and so
1063 on. High-level functionality is dealt with in the next chapter
1064 (@pxref{Decoding and Viewing}).
1067 * rfc2045:: Encoding @code{Content-Type} headers.
1068 * rfc2231:: Parsing @code{Content-Type} headers.
1069 * ietf-drums:: Handling mail headers defined by RFC822bis.
1070 * rfc2047:: En/decoding encoded words in headers.
1071 * time-date:: Functions for parsing dates and manipulating time.
1072 * qp:: Quoted-Printable en/decoding.
1073 * base64:: Base64 en/decoding.
1074 * binhex:: Binhex decoding.
1075 * uudecode:: Uuencode decoding.
1076 * yenc:: Yenc decoding.
1077 * rfc1843:: Decoding HZ-encoded text.
1078 * mailcap:: How parts are displayed is specified by the @file{.mailcap} file
1085 RFC2045 is the ``main'' @sc{mime} document, and as such, one would
1086 imagine that there would be a lot to implement. But there isn't, since
1087 most of the implementation details are delegated to the subsequent
1090 So @file{rfc2045.el} has only a single function:
1093 @item rfc2045-encode-string
1094 @findex rfc2045-encode-string
1095 Takes a parameter and a value and returns a @samp{PARAM=VALUE} string.
1096 @var{value} will be quoted if there are non-safe characters in it.
1103 RFC2231 defines a syntax for the @code{Content-Type} and
1104 @code{Content-Disposition} headers. Its snappy name is @dfn{MIME
1105 Parameter Value and Encoded Word Extensions: Character Sets, Languages,
1108 In short, these headers look something like this:
1111 Content-Type: application/x-stuff;
1112 title*0*=us-ascii'en'This%20is%20even%20more%20;
1113 title*1*=%2A%2A%2Afun%2A%2A%2A%20;
1117 They usually aren't this bad, though.
1119 The following functions are defined by this library:
1122 @item rfc2231-parse-string
1123 @findex rfc2231-parse-string
1124 Parse a @code{Content-Type} header and return a list describing its
1128 (rfc2231-parse-string
1129 "application/x-stuff;
1130 title*0*=us-ascii'en'This%20is%20even%20more%20;
1131 title*1*=%2A%2A%2Afun%2A%2A%2A%20;
1132 title*2=\"isn't it!\"")
1133 @result{} ("application/x-stuff"
1134 (title . "This is even more ***fun*** isn't it!"))
1137 @item rfc2231-get-value
1138 @findex rfc2231-get-value
1139 Takes one of the lists on the format above and returns
1140 the value of the specified attribute.
1142 @item rfc2231-encode-string
1143 @findex rfc2231-encode-string
1144 Encode a parameter in headers likes @code{Content-Type} and
1145 @code{Content-Disposition}.
1153 @dfn{drums} is an IETF working group that is working on the replacement
1156 The functions provided by this library include:
1159 @item ietf-drums-remove-comments
1160 @findex ietf-drums-remove-comments
1161 Remove the comments from the argument and return the results.
1163 @item ietf-drums-remove-whitespace
1164 @findex ietf-drums-remove-whitespace
1165 Remove linear white space from the string and return the results.
1166 Spaces inside quoted strings and comments are left untouched.
1168 @item ietf-drums-get-comment
1169 @findex ietf-drums-get-comment
1170 Return the last most comment from the string.
1172 @item ietf-drums-parse-address
1173 @findex ietf-drums-parse-address
1174 Parse an address string and return a list that contains the mailbox and
1175 the plain text name.
1177 @item ietf-drums-parse-addresses
1178 @findex ietf-drums-parse-addresses
1179 Parse a string that contains any number of comma-separated addresses and
1180 return a list that contains mailbox/plain text pairs.
1182 @item ietf-drums-parse-date
1183 @findex ietf-drums-parse-date
1184 Parse a date string and return an Emacs time structure.
1186 @item ietf-drums-narrow-to-header
1187 @findex ietf-drums-narrow-to-header
1188 Narrow the buffer to the header section of the current buffer.
1196 RFC2047 (Message Header Extensions for Non-ASCII Text) specifies how
1197 non-ASCII text in headers are to be encoded. This is actually rather
1198 complicated, so a number of variables are necessary to tweak what this
1201 The following variables are tweakable:
1204 @item rfc2047-default-charset
1205 @vindex rfc2047-default-charset
1206 Characters in this charset should not be decoded by this library.
1207 This defaults to @code{iso-8859-1}.
1209 @item rfc2047-header-encoding-alist
1210 @vindex rfc2047-header-encoding-alist
1211 This is an alist of header / encoding-type pairs. Its main purpose is
1212 to prevent encoding of certain headers.
1214 The keys can either be header regexps, or @code{t}.
1216 The values can be either @code{nil}, in which case the header(s) in
1217 question won't be encoded, or @code{mime}, which means that they will be
1220 @item rfc2047-charset-encoding-alist
1221 @vindex rfc2047-charset-encoding-alist
1222 RFC2047 specifies two forms of encoding---@code{Q} (a
1223 Quoted-Printable-like encoding) and @code{B} (base64). This alist
1224 specifies which charset should use which encoding.
1226 @item rfc2047-encoding-function-alist
1227 @vindex rfc2047-encoding-function-alist
1228 This is an alist of encoding / function pairs. The encodings are
1229 @code{Q}, @code{B} and @code{nil}.
1231 @item rfc2047-q-encoding-alist
1232 @vindex rfc2047-q-encoding-alist
1233 The @code{Q} encoding isn't quite the same for all headers. Some
1234 headers allow a narrower range of characters, and that is what this
1235 variable is for. It's an alist of header regexps / allowable character
1238 @item rfc2047-encoded-word-regexp
1239 @vindex rfc2047-encoded-word-regexp
1240 When decoding words, this library looks for matches to this regexp.
1244 Those were the variables, and these are this functions:
1247 @item rfc2047-narrow-to-field
1248 @findex rfc2047-narrow-to-field
1249 Narrow the buffer to the header on the current line.
1251 @item rfc2047-encode-message-header
1252 @findex rfc2047-encode-message-header
1253 Should be called narrowed to the header of a message. Encodes according
1254 to @code{rfc2047-header-encoding-alist}.
1256 @item rfc2047-encode-region
1257 @findex rfc2047-encode-region
1258 Encodes all encodable words in the region specified.
1260 @item rfc2047-encode-string
1261 @findex rfc2047-encode-string
1262 Encode a string and return the results.
1264 @item rfc2047-decode-region
1265 @findex rfc2047-decode-region
1266 Decode the encoded words in the region.
1268 @item rfc2047-decode-string
1269 @findex rfc2047-decode-string
1270 Decode a string and return the results.
1278 While not really a part of the @sc{mime} library, it is convenient to
1279 document this library here. It deals with parsing @code{Date} headers
1280 and manipulating time. (Not by using tesseracts, though, I'm sorry to
1283 These functions convert between five formats: A date string, an Emacs
1284 time structure, a decoded time list, a second number, and a day number.
1286 Here's a bunch of time/date/second/day examples:
1289 (parse-time-string "Sat Sep 12 12:21:54 1998 +0200")
1290 @result{} (54 21 12 12 9 1998 6 nil 7200)
1292 (date-to-time "Sat Sep 12 12:21:54 1998 +0200")
1293 @result{} (13818 19266)
1295 (time-to-seconds '(13818 19266))
1296 @result{} 905595714.0
1298 (seconds-to-time 905595714.0)
1299 @result{} (13818 19266 0)
1301 (time-to-days '(13818 19266))
1304 (days-to-time 729644)
1305 @result{} (961933 65536)
1307 (time-since '(13818 19266))
1310 (time-less-p '(13818 19266) '(13818 19145))
1313 (subtract-time '(13818 19266) '(13818 19145))
1316 (days-between "Sat Sep 12 12:21:54 1998 +0200"
1317 "Sat Sep 07 12:21:54 1998 +0200")
1320 (date-leap-year-p 2000)
1323 (time-to-day-in-year '(13818 19266))
1326 (time-to-number-of-days
1328 (date-to-time "Mon, 01 Jan 2001 02:22:26 GMT")))
1329 @result{} 4.146122685185185
1332 And finally, we have @code{safe-date-to-time}, which does the same as
1333 @code{date-to-time}, but returns a zero time if the date is
1334 syntactically malformed.
1336 The five data representations used are the following:
1340 An RFC822 (or similar) date string. For instance: @code{"Sat Sep 12
1341 12:21:54 1998 +0200"}.
1344 An internal Emacs time. For instance: @code{(13818 26466)}.
1347 A floating point representation of the internal Emacs time. For
1348 instance: @code{905595714.0}.
1351 An integer number representing the number of days since 00000101. For
1352 instance: @code{729644}.
1355 A list of decoded time. For instance: @code{(54 21 12 12 9 1998 6 t
1359 All the examples above represent the same moment.
1361 These are the functions available:
1365 Take a date and return a time.
1367 @item time-to-seconds
1368 Take a time and return seconds.
1370 @item seconds-to-time
1371 Take seconds and return a time.
1374 Take a time and return days.
1377 Take days and return a time.
1380 Take a date and return days.
1382 @item time-to-number-of-days
1383 Take a time and return the number of days that represents.
1385 @item safe-date-to-time
1386 Take a date and return a time. If the date is not syntactically valid,
1387 return a "zero" date.
1390 Take two times and say whether the first time is less (i. e., earlier)
1391 than the second time.
1394 Take a time and return a time saying how long it was since that time.
1397 Take two times and subtract the second from the first. I. e., return
1398 the time between the two times.
1401 Take two days and return the number of days between those two days.
1403 @item date-leap-year-p
1404 Take a year number and say whether it's a leap year.
1406 @item time-to-day-in-year
1407 Take a time and return the day number within the year that the time is
1416 This library deals with decoding and encoding Quoted-Printable text.
1418 Very briefly explained, qp encoding means translating all 8-bit
1419 characters (and lots of control characters) into things that look like
1420 @samp{=EF}; that is, an equal sign followed by the byte encoded as a hex
1423 The following functions are defined by the library:
1426 @item quoted-printable-decode-region
1427 @findex quoted-printable-decode-region
1428 QP-decode all the encoded text in the specified region.
1430 @item quoted-printable-decode-string
1431 @findex quoted-printable-decode-string
1432 Decode the QP-encoded text in a string and return the results.
1434 @item quoted-printable-encode-region
1435 @findex quoted-printable-encode-region
1436 QP-encode all the encodable characters in the specified region. The third
1437 optional parameter @var{fold} specifies whether to fold long lines.
1438 (Long here means 72.)
1440 @item quoted-printable-encode-string
1441 @findex quoted-printable-encode-string
1442 QP-encode all the encodable characters in a string and return the
1452 Base64 is an encoding that encodes three bytes into four characters,
1453 thereby increasing the size by about 33%. The alphabet used for
1454 encoding is very resistant to mangling during transit.
1456 The following functions are defined by this library:
1459 @item base64-encode-region
1460 @findex base64-encode-region
1461 base64 encode the selected region. Return the length of the encoded
1462 text. Optional third argument @var{no-line-break} means do not break
1463 long lines into shorter lines.
1465 @item base64-encode-string
1466 @findex base64-encode-string
1467 base64 encode a string and return the result.
1469 @item base64-decode-region
1470 @findex base64-decode-region
1471 base64 decode the selected region. Return the length of the decoded
1472 text. If the region can't be decoded, return @code{nil} and don't
1475 @item base64-decode-string
1476 @findex base64-decode-string
1477 base64 decode a string and return the result. If the string can't be
1478 decoded, @code{nil} is returned.
1489 @code{binhex} is an encoding that originated in Macintosh environments.
1490 The following function is supplied to deal with these:
1493 @item binhex-decode-region
1494 @findex binhex-decode-region
1495 Decode the encoded text in the region. If given a third parameter, only
1496 decode the @code{binhex} header and return the filename.
1505 @code{uuencode} is probably still the most popular encoding of binaries
1506 used on Usenet, although @code{base64} rules the mail world.
1508 The following function is supplied by this package:
1511 @item uudecode-decode-region
1512 @findex uudecode-decode-region
1513 Decode the text in the region.
1521 @code{yenc} is used for encoding binaries on Usenet. The following
1522 function is supplied by this package:
1525 @item yenc-decode-region
1526 @findex yenc-decode-region
1527 Decode the encoded text in the region.
1538 RFC1843 deals with mixing Chinese and ASCII characters in messages. In
1539 essence, RFC1843 switches between ASCII and Chinese by doing this:
1542 This sentence is in ASCII.
1543 The next sentence is in GB.~@{<:Ky2;S@{#,NpJ)l6HK!#~@}Bye.
1546 Simple enough, and widely used in China.
1548 The following functions are available to handle this encoding:
1551 @item rfc1843-decode-region
1552 Decode HZ-encoded text in the region.
1554 @item rfc1843-decode-string
1555 Decode a HZ-encoded string and return the result.
1563 The @file{~/.mailcap} file is parsed by most @sc{mime}-aware message
1564 handlers and describes how elements are supposed to be displayed.
1565 Here's an example file:
1569 audio/wav; wavplayer %s
1570 application/msword; catdoc %s ; copiousoutput ; nametemplate=%s.doc
1573 This says that all image files should be displayed with @code{gimp},
1574 that WAVE audio files should be played by @code{wavplayer}, and that
1575 MS-WORD files should be inlined by @code{catdoc}.
1577 The @code{mailcap} library parses this file, and provides functions for
1581 @item mailcap-mime-data
1582 @vindex mailcap-mime-data
1583 This variable is an alist of alists containing backup viewing rules.
1587 Interface functions:
1590 @item mailcap-parse-mailcaps
1591 @findex mailcap-parse-mailcaps
1592 Parse the @code{~/.mailcap} file.
1594 @item mailcap-mime-info
1595 Takes a @sc{mime} type as its argument and returns the matching viewer.
1605 The Emacs @sc{mime} library implements handling of various elements
1606 according to a (somewhat) large number of RFCs, drafts and standards
1607 documents. This chapter lists the relevant ones. They can all be
1608 fetched from @uref{http://quimby.gnus.org/notes/}.
1613 Standard for the Format of ARPA Internet Text Messages.
1616 Standard for Interchange of USENET Messages
1619 Format of Internet Message Bodies
1625 Message Header Extensions for Non-ASCII Text
1628 Registration Procedures
1631 Conformance Criteria and Examples
1634 @sc{mime} Parameter Value and Encoded Word Extensions: Character Sets,
1635 Languages, and Continuations
1638 HZ - A Data Format for Exchanging Files of Arbitrarily Mixed Chinese and
1641 @item draft-ietf-drums-msg-fmt-05.txt
1642 Draft for the successor of RFC822
1645 The @sc{mime} Multipart/Related Content-type
1648 The Multipart/Report Content Type for the Reporting of Mail System
1649 Administrative Messages
1652 Communicating Presentation Information in Internet Messages: The
1653 Content-Disposition Header Field
1656 Documentation of the text/plain format parameter for flowed text.
1672 @c coding: iso-8859-1