summaryrefslogtreecommitdiff
path: root/tools/write-mime-multipart
blob: 99cfe299acf108ebafa8ae5f6d456355278e6063 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python

"""Send the contents of a directory as a MIME message."""

import os
import sys
import smtplib
# For guessing MIME type based on file name extension
import mimetypes

from optparse import OptionParser

from email import encoders
from email.message import Message
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

COMMASPACE = ', '


def main():
    outer = MIMEMultipart()
    #outer['Subject'] = 'Contents of directory %s' % os.path.abspath(directory)
    #outer['To'] = COMMASPACE.join(opts.recipients)
    #outer['From'] = opts.sender
    #outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'

    if len(sys.argv) % 2 != 1 or len(sys.argv) == 1:
        print "Usage: prog content-type file1 [ content-type2 file2 [ ... ] ]"
        sys.exit(1)

    pos = 1
    while pos < len(sys.argv):
        ctype = sys.argv[pos]
        path = sys.argv[pos+1]
        pos=pos+2

        maintype, subtype = ctype.split('/', 1)
        if maintype == 'text':
            fp = open(path)
            # Note: we should handle calculating the charset
            msg = MIMEText(fp.read(), _subtype=subtype)
            fp.close()
        else:
            fp = open(path, 'rb')
            msg = MIMEBase(maintype, subtype)
            msg.set_payload(fp.read())
            fp.close()
            # Encode the payload using Base64
            encoders.encode_base64(msg)

        # Set the filename parameter
        msg.add_header('Content-Disposition', 'attachment',
            filename=os.path.basename(path))

        if ctype is None:
            # No guess could be made, or the file is encoded (compressed), so
            # use a generic bag-of-bits type.
            ctype = 'application/octet-stream'
        outer.attach(msg)
    # Now send or store the message
    #composed = outer.as_string()
    sys.stdout.write(outer.as_string())

if __name__ == '__main__':
    main()