Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
ea66abc6
Commit
ea66abc6
authored
Oct 01, 2002
by
Barry Warsaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleaned up the examples.
parent
5db478fa
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
27 deletions
+22
-27
Doc/lib/email.tex
Doc/lib/email.tex
+22
-27
No files found.
Doc/lib/email.tex
View file @
ea66abc6
...
...
@@ -289,15 +289,14 @@ First, let's see how to create and send a simple text message:
# Import smtplib for the actual sending function
import smtplib
# Here are the email pacakge modules we'll need
from email import Encoders
# Import the email modules we'll need
from email.MIMEText import MIMEText
# Open a plain text file for reading
fp = open(textfile)
# Create a text/plain message, using Quoted-Printable encoding for non-ASCII
#
characters.
msg = MIMEText(fp.read()
,
_
encoder=Encoders.encode
_
quopri
)
# Open a plain text file for reading
. For this example, assume that
# the text file contains only ASCII characters.
fp = open(textfile, 'rb')
#
Create a text/plain message
msg = MIMEText(fp.read())
fp.close()
# me == the sender's email address
...
...
@@ -306,16 +305,16 @@ msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you
# Send the message via our own SMTP server
. Use msg.as
_
string() with
#
unixfrom=0 so as not to confuse SMTP
.
# Send the message via our own SMTP server
, but don't include the
#
envelope header
.
s = smtplib.SMTP()
s.connect()
s.sendmail(me, [you], msg.as
_
string(
0
))
s.sendmail(me, [you], msg.as
_
string())
s.close()
\end{verbatim}
Here's an example of how to send a MIME message containing a bunch of
family pictures:
family pictures
that may be residing in a directory
:
\begin{verbatim}
# Import smtplib for the actual sending function
...
...
@@ -323,15 +322,15 @@ import smtplib
# Here are the email pacakge modules we'll need
from email.MIMEImage import MIMEImage
from email.MIME
Base import MIMEBase
from email.MIME
Multipart import MIMEMultipart
COMMASPACE = ', '
# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'Our family reunion'
# me == the sender's email address
# family = the list of all recipients' email addresses
msg = MIMEBase('multipart', 'mixed')
msg['Subject'] = 'Our family reunion'
msg['From'] = me
msg['To'] = COMMASPACE.join(family)
msg.preamble = 'Our family reunion'
...
...
@@ -340,7 +339,7 @@ msg.epilogue = ''
# Assume we know that the image files are all in PNG format
for file in pngfiles:
# Open the files in binary mode. Let the MIMEI
M
age class automatically
# Open the files in binary mode. Let the MIMEI
m
age class automatically
# guess the specific image type.
fp = open(file, 'rb')
img = MIMEImage(fp.read())
...
...
@@ -350,7 +349,7 @@ for file in pngfiles:
# Send the email via our own SMTP server.
s = smtplib.SMTP()
s.connect()
s.sendmail(me, family, msg.as
_
string(
unixfrom=0
))
s.sendmail(me, family, msg.as
_
string())
s.close()
\end{verbatim}
...
...
@@ -394,7 +393,7 @@ import mimetypes
from email import Encoders
from email.Message import Message
from email.MIMEAudio import MIMEAudio
from email.MIME
Base import MIMEBase
from email.MIME
Multipart import MIMEMultipart
from email.MIMEImage import MIMEImage
from email.MIMEText import MIMEText
...
...
@@ -428,7 +427,7 @@ def main():
recips = args[1:]
# Create the enclosing (outer) message
outer = MIME
Base('multipart', 'mixed'
)
outer = MIME
Multipart(
)
outer['Subject'] = 'Contents of directory
%s' % os.path.abspath(dir)
outer['To'] = COMMASPACE.join(recips)
outer['From'] = sender
...
...
@@ -440,9 +439,9 @@ def main():
path = os.path.join(dir, filename)
if not os.path.isfile(path):
continue
# Guess the
Content-Type:
based on the file's extension. Encoding
# Guess the
content type
based on the file's extension. Encoding
# will be ignored, although we should check for simple things like
# gzip'd or compressed files
# gzip'd or compressed files
.
ctype, encoding = mimetypes.guess
_
type(path)
if ctype is None or encoding is not None:
# No guess could be made, or the file is encoded (compressed), so
...
...
@@ -465,7 +464,7 @@ def main():
else:
fp = open(path, 'rb')
msg = MIMEBase(maintype, subtype)
msg.
add
_
payload(fp.read())
msg.
set
_
payload(fp.read())
fp.close()
# Encode the payload using Base64
Encoders.encode
_
base64(msg)
...
...
@@ -473,14 +472,10 @@ def main():
msg.add
_
header('Content-Disposition', 'attachment', filename=filename)
outer.attach(msg)
fp = open('/tmp/debug.pck', 'w')
import cPickle
cPickle.dump(outer, fp)
fp.close()
# Now send the message
s = smtplib.SMTP()
s.connect()
s.sendmail(sender, recips, outer.as
_
string(
0
))
s.sendmail(sender, recips, outer.as
_
string())
s.close()
...
...
@@ -556,7 +551,7 @@ def main():
counter = 1
for part in msg.walk():
# multipart/* are just containers
if part.get
_
main
_
type() == 'multipart':
if part.get
_
content
_
main
type() == 'multipart':
continue
# Applications should really sanitize the given filename so that an
# email message can't be used to overwrite important files
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment