| 1 | """Generic MIME writer.
|
|---|
| 2 |
|
|---|
| 3 | This module defines the class MimeWriter. The MimeWriter class implements
|
|---|
| 4 | a basic formatter for creating MIME multi-part files. It doesn't seek around
|
|---|
| 5 | the output file nor does it use large amounts of buffer space. You must write
|
|---|
| 6 | the parts out in the order that they should occur in the final file.
|
|---|
| 7 | MimeWriter does buffer the headers you add, allowing you to rearrange their
|
|---|
| 8 | order.
|
|---|
| 9 |
|
|---|
| 10 | """
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 | import mimetools
|
|---|
| 14 |
|
|---|
| 15 | __all__ = ["MimeWriter"]
|
|---|
| 16 |
|
|---|
| 17 | class MimeWriter:
|
|---|
| 18 |
|
|---|
| 19 | """Generic MIME writer.
|
|---|
| 20 |
|
|---|
| 21 | Methods:
|
|---|
| 22 |
|
|---|
| 23 | __init__()
|
|---|
| 24 | addheader()
|
|---|
| 25 | flushheaders()
|
|---|
| 26 | startbody()
|
|---|
| 27 | startmultipartbody()
|
|---|
| 28 | nextpart()
|
|---|
| 29 | lastpart()
|
|---|
| 30 |
|
|---|
| 31 | A MIME writer is much more primitive than a MIME parser. It
|
|---|
| 32 | doesn't seek around on the output file, and it doesn't use large
|
|---|
| 33 | amounts of buffer space, so you have to write the parts in the
|
|---|
| 34 | order they should occur on the output file. It does buffer the
|
|---|
| 35 | headers you add, allowing you to rearrange their order.
|
|---|
| 36 |
|
|---|
| 37 | General usage is:
|
|---|
| 38 |
|
|---|
| 39 | f = <open the output file>
|
|---|
| 40 | w = MimeWriter(f)
|
|---|
| 41 | ...call w.addheader(key, value) 0 or more times...
|
|---|
| 42 |
|
|---|
| 43 | followed by either:
|
|---|
| 44 |
|
|---|
| 45 | f = w.startbody(content_type)
|
|---|
| 46 | ...call f.write(data) for body data...
|
|---|
| 47 |
|
|---|
| 48 | or:
|
|---|
| 49 |
|
|---|
| 50 | w.startmultipartbody(subtype)
|
|---|
| 51 | for each part:
|
|---|
| 52 | subwriter = w.nextpart()
|
|---|
| 53 | ...use the subwriter's methods to create the subpart...
|
|---|
| 54 | w.lastpart()
|
|---|
|
|---|