09.11.2016 Views

Foundations of Python Network Programming 978-1-4302-3004-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 12 ■ E-MAIL COMPOSITION AND DECODING<br />

» mimetype, mimeencoding = mimetypes.guess_type(filename)<br />

» if mimeencoding or (mimetype is None):<br />

» » mimetype = 'application/octet-stream'<br />

» maintype, subtype = mimetype.split('/')<br />

» if maintype == 'text':<br />

» » retval = MIMEText(fd.read(), _subtype=subtype)<br />

» else:<br />

» » retval = MIMEBase(maintype, subtype)<br />

» » retval.set_payload(fd.read())<br />

» » encoders.encode_base64(retval)<br />

» retval.add_header('Content-Disposition', 'attachment',<br />

» » » filename = filename)<br />

» fd.close()<br />

» return retval<br />

message = """Hello,<br />

This is a test message from Chapter 12. I hope you enjoy it!<br />

-- Anonymous"""<br />

msg = MIMEMultipart()<br />

msg['To'] = 'recipient@example.com'<br />

msg['From'] = 'Test Sender '<br />

msg['Subject'] = 'Test Message, Chapter 12'<br />

msg['Date'] = utils.formatdate(localtime = 1)<br />

msg['Message-ID'] = utils.make_msgid()<br />

body = MIMEText(message, _subtype='plain')<br />

msg.attach(body)<br />

for filename in sys.argv[1:]:<br />

» msg.attach(attachment(filename))<br />

print msg.as_string()<br />

The attachment() function does the work <strong>of</strong> creating a message attachment object. First, it determines<br />

the MIME type <strong>of</strong> each file by using <strong>Python</strong>’s built-in mimetypes module. If the type can’t be determined, or<br />

it will need a special kind <strong>of</strong> encoding, then a type is declared that promises only that the data is made <strong>of</strong> a<br />

“stream <strong>of</strong> octets” (sequence <strong>of</strong> bytes) but without any further promise about what they mean.<br />

If the file is a text document whose MIME type starts with text/, a MIMEText object is created to handle<br />

it; otherwise, a MIMEBase generic object is created. In the latter case, the contents are assumed to be binary,<br />

so they are encoded with base-64. Finally, an appropriate Content-Disposition header is added to that<br />

section <strong>of</strong> the MIME file so that mail readers will know that they are dealing with an attachment.<br />

The result <strong>of</strong> running this program is shown in Listing 12–9.<br />

Listing 12–9. Running the Program in Listing 12–8<br />

$ echo "This is a test" > test.txt<br />

$ gzip < test.txt > test.txt.gz<br />

$ ./mime_gen_basic.py test.txt test.txt.gz<br />

Content-Type: multipart/mixed; boundary="===============1623374356=="<br />

MIME-Version: 1.0<br />

To: recipient@example.com<br />

From: Test Sender <br />

Subject: Test Message, Chapter 12<br />

Date: Thu, 11 Dec 2003 16:00:55 -0600<br />

207

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!