Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea66abc

Browse files
committed
Cleaned up the examples.
1 parent 5db478f commit ea66abc

1 file changed

Lines changed: 22 additions & 27 deletions

File tree

Doc/lib/email.tex

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,14 @@ \subsection{Examples}
289289
# Import smtplib for the actual sending function
290290
import smtplib
291291
292-
# Here are the email pacakge modules we'll need
293-
from email import Encoders
292+
# Import the email modules we'll need
294293
from email.MIMEText import MIMEText
295294
296-
# Open a plain text file for reading
297-
fp = open(textfile)
298-
# Create a text/plain message, using Quoted-Printable encoding for non-ASCII
299-
# characters.
300-
msg = MIMEText(fp.read(), _encoder=Encoders.encode_quopri)
295+
# Open a plain text file for reading. For this example, assume that
296+
# the text file contains only ASCII characters.
297+
fp = open(textfile, 'rb')
298+
# Create a text/plain message
299+
msg = MIMEText(fp.read())
301300
fp.close()
302301
303302
# me == the sender's email address
@@ -306,32 +305,32 @@ \subsection{Examples}
306305
msg['From'] = me
307306
msg['To'] = you
308307
309-
# Send the message via our own SMTP server. Use msg.as_string() with
310-
# unixfrom=0 so as not to confuse SMTP.
308+
# Send the message via our own SMTP server, but don't include the
309+
# envelope header.
311310
s = smtplib.SMTP()
312311
s.connect()
313-
s.sendmail(me, [you], msg.as_string(0))
312+
s.sendmail(me, [you], msg.as_string())
314313
s.close()
315314
\end{verbatim}
316315

317316
Here's an example of how to send a MIME message containing a bunch of
318-
family pictures:
317+
family pictures that may be residing in a directory:
319318

320319
\begin{verbatim}
321320
# Import smtplib for the actual sending function
322321
import smtplib
323322
324323
# Here are the email pacakge modules we'll need
325324
from email.MIMEImage import MIMEImage
326-
from email.MIMEBase import MIMEBase
325+
from email.MIMEMultipart import MIMEMultipart
327326
328327
COMMASPACE = ', '
329328
330329
# Create the container (outer) email message.
330+
msg = MIMEMultipart()
331+
msg['Subject'] = 'Our family reunion'
331332
# me == the sender's email address
332333
# family = the list of all recipients' email addresses
333-
msg = MIMEBase('multipart', 'mixed')
334-
msg['Subject'] = 'Our family reunion'
335334
msg['From'] = me
336335
msg['To'] = COMMASPACE.join(family)
337336
msg.preamble = 'Our family reunion'
@@ -340,7 +339,7 @@ \subsection{Examples}
340339
341340
# Assume we know that the image files are all in PNG format
342341
for file in pngfiles:
343-
# Open the files in binary mode. Let the MIMEIMage class automatically
342+
# Open the files in binary mode. Let the MIMEImage class automatically
344343
# guess the specific image type.
345344
fp = open(file, 'rb')
346345
img = MIMEImage(fp.read())
@@ -350,7 +349,7 @@ \subsection{Examples}
350349
# Send the email via our own SMTP server.
351350
s = smtplib.SMTP()
352351
s.connect()
353-
s.sendmail(me, family, msg.as_string(unixfrom=0))
352+
s.sendmail(me, family, msg.as_string())
354353
s.close()
355354
\end{verbatim}
356355

@@ -394,7 +393,7 @@ \subsection{Examples}
394393
from email import Encoders
395394
from email.Message import Message
396395
from email.MIMEAudio import MIMEAudio
397-
from email.MIMEBase import MIMEBase
396+
from email.MIMEMultipart import MIMEMultipart
398397
from email.MIMEImage import MIMEImage
399398
from email.MIMEText import MIMEText
400399
@@ -428,7 +427,7 @@ \subsection{Examples}
428427
recips = args[1:]
429428
430429
# Create the enclosing (outer) message
431-
outer = MIMEBase('multipart', 'mixed')
430+
outer = MIMEMultipart()
432431
outer['Subject'] = 'Contents of directory %s' % os.path.abspath(dir)
433432
outer['To'] = COMMASPACE.join(recips)
434433
outer['From'] = sender
@@ -440,9 +439,9 @@ \subsection{Examples}
440439
path = os.path.join(dir, filename)
441440
if not os.path.isfile(path):
442441
continue
443-
# Guess the Content-Type: based on the file's extension. Encoding
442+
# Guess the content type based on the file's extension. Encoding
444443
# will be ignored, although we should check for simple things like
445-
# gzip'd or compressed files
444+
# gzip'd or compressed files.
446445
ctype, encoding = mimetypes.guess_type(path)
447446
if ctype is None or encoding is not None:
448447
# No guess could be made, or the file is encoded (compressed), so
@@ -465,22 +464,18 @@ \subsection{Examples}
465464
else:
466465
fp = open(path, 'rb')
467466
msg = MIMEBase(maintype, subtype)
468-
msg.add_payload(fp.read())
467+
msg.set_payload(fp.read())
469468
fp.close()
470469
# Encode the payload using Base64
471470
Encoders.encode_base64(msg)
472471
# Set the filename parameter
473472
msg.add_header('Content-Disposition', 'attachment', filename=filename)
474473
outer.attach(msg)
475474
476-
fp = open('/tmp/debug.pck', 'w')
477-
import cPickle
478-
cPickle.dump(outer, fp)
479-
fp.close()
480475
# Now send the message
481476
s = smtplib.SMTP()
482477
s.connect()
483-
s.sendmail(sender, recips, outer.as_string(0))
478+
s.sendmail(sender, recips, outer.as_string())
484479
s.close()
485480
486481
@@ -556,7 +551,7 @@ \subsection{Examples}
556551
counter = 1
557552
for part in msg.walk():
558553
# multipart/* are just containers
559-
if part.get_main_type() == 'multipart':
554+
if part.get_content_maintype() == 'multipart':
560555
continue
561556
# Applications should really sanitize the given filename so that an
562557
# email message can't be used to overwrite important files

0 commit comments

Comments
 (0)