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
27ccb7f8
Commit
27ccb7f8
authored
Oct 01, 2002
by
Barry Warsaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleaned up the examples.
parent
2666fea2
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 @
27ccb7f8
...
@@ -289,15 +289,14 @@ First, let's see how to create and send a simple text message:
...
@@ -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 for the actual sending function
import smtplib
import smtplib
# Here are the email pacakge modules we'll need
# Import the email modules we'll need
from email import Encoders
from email.MIMEText import MIMEText
from email.MIMEText import MIMEText
# Open a plain text file for reading
# Open a plain text file for reading
. For this example, assume that
fp = open(textfile)
# the text file contains only ASCII characters.
# Create a text/plain message, using Quoted-Printable encoding for non-ASCII
fp = open(textfile, 'rb')
#
characters.
#
Create a text/plain message
msg = MIMEText(fp.read()
,
_
encoder=Encoders.encode
_
quopri
)
msg = MIMEText(fp.read())
fp.close()
fp.close()
# me == the sender's email address
# me == the sender's email address
...
@@ -306,16 +305,16 @@ msg['Subject'] = 'The contents of %s' % textfile
...
@@ -306,16 +305,16 @@ msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['From'] = me
msg['To'] = you
msg['To'] = you
# Send the message via our own SMTP server
. Use msg.as
_
string() with
# Send the message via our own SMTP server
, but don't include the
#
unixfrom=0 so as not to confuse SMTP
.
#
envelope header
.
s = smtplib.SMTP()
s = smtplib.SMTP()
s.connect()
s.connect()
s.sendmail(me, [you], msg.as
_
string(
0
))
s.sendmail(me, [you], msg.as
_
string())
s.close()
s.close()
\end{verbatim}
\end{verbatim}
Here's an example of how to send a MIME message containing a bunch of
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}
\begin{verbatim}
# Import smtplib for the actual sending function
# Import smtplib for the actual sending function
...
@@ -323,15 +322,15 @@ import smtplib
...
@@ -323,15 +322,15 @@ import smtplib
# Here are the email pacakge modules we'll need
# Here are the email pacakge modules we'll need
from email.MIMEImage import MIMEImage
from email.MIMEImage import MIMEImage
from email.MIME
Base import MIMEBase
from email.MIME
Multipart import MIMEMultipart
COMMASPACE = ', '
COMMASPACE = ', '
# Create the container (outer) email message.
# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'Our family reunion'
# me == the sender's email address
# me == the sender's email address
# family = the list of all recipients' email addresses
# family = the list of all recipients' email addresses
msg = MIMEBase('multipart', 'mixed')
msg['Subject'] = 'Our family reunion'
msg['From'] = me
msg['From'] = me
msg['To'] = COMMASPACE.join(family)
msg['To'] = COMMASPACE.join(family)
msg.preamble = 'Our family reunion'
msg.preamble = 'Our family reunion'
...
@@ -340,7 +339,7 @@ msg.epilogue = ''
...
@@ -340,7 +339,7 @@ msg.epilogue = ''
# Assume we know that the image files are all in PNG format
# Assume we know that the image files are all in PNG format
for file in pngfiles:
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.
# guess the specific image type.
fp = open(file, 'rb')
fp = open(file, 'rb')
img = MIMEImage(fp.read())
img = MIMEImage(fp.read())
...
@@ -350,7 +349,7 @@ for file in pngfiles:
...
@@ -350,7 +349,7 @@ for file in pngfiles:
# Send the email via our own SMTP server.
# Send the email via our own SMTP server.
s = smtplib.SMTP()
s = smtplib.SMTP()
s.connect()
s.connect()
s.sendmail(me, family, msg.as
_
string(
unixfrom=0
))
s.sendmail(me, family, msg.as
_
string())
s.close()
s.close()
\end{verbatim}
\end{verbatim}
...
@@ -394,7 +393,7 @@ import mimetypes
...
@@ -394,7 +393,7 @@ import mimetypes
from email import Encoders
from email import Encoders
from email.Message import Message
from email.Message import Message
from email.MIMEAudio import MIMEAudio
from email.MIMEAudio import MIMEAudio
from email.MIME
Base import MIMEBase
from email.MIME
Multipart import MIMEMultipart
from email.MIMEImage import MIMEImage
from email.MIMEImage import MIMEImage
from email.MIMEText import MIMEText
from email.MIMEText import MIMEText
...
@@ -428,7 +427,7 @@ def main():
...
@@ -428,7 +427,7 @@ def main():
recips = args[1:]
recips = args[1:]
# Create the enclosing (outer) message
# Create the enclosing (outer) message
outer = MIME
Base('multipart', 'mixed'
)
outer = MIME
Multipart(
)
outer['Subject'] = 'Contents of directory
%s' % os.path.abspath(dir)
outer['Subject'] = 'Contents of directory
%s' % os.path.abspath(dir)
outer['To'] = COMMASPACE.join(recips)
outer['To'] = COMMASPACE.join(recips)
outer['From'] = sender
outer['From'] = sender
...
@@ -440,9 +439,9 @@ def main():
...
@@ -440,9 +439,9 @@ def main():
path = os.path.join(dir, filename)
path = os.path.join(dir, filename)
if not os.path.isfile(path):
if not os.path.isfile(path):
continue
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
# 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)
ctype, encoding = mimetypes.guess
_
type(path)
if ctype is None or encoding is not None:
if ctype is None or encoding is not None:
# No guess could be made, or the file is encoded (compressed), so
# No guess could be made, or the file is encoded (compressed), so
...
@@ -465,7 +464,7 @@ def main():
...
@@ -465,7 +464,7 @@ def main():
else:
else:
fp = open(path, 'rb')
fp = open(path, 'rb')
msg = MIMEBase(maintype, subtype)
msg = MIMEBase(maintype, subtype)
msg.
add
_
payload(fp.read())
msg.
set
_
payload(fp.read())
fp.close()
fp.close()
# Encode the payload using Base64
# Encode the payload using Base64
Encoders.encode
_
base64(msg)
Encoders.encode
_
base64(msg)
...
@@ -473,14 +472,10 @@ def main():
...
@@ -473,14 +472,10 @@ def main():
msg.add
_
header('Content-Disposition', 'attachment', filename=filename)
msg.add
_
header('Content-Disposition', 'attachment', filename=filename)
outer.attach(msg)
outer.attach(msg)
fp = open('/tmp/debug.pck', 'w')
import cPickle
cPickle.dump(outer, fp)
fp.close()
# Now send the message
# Now send the message
s = smtplib.SMTP()
s = smtplib.SMTP()
s.connect()
s.connect()
s.sendmail(sender, recips, outer.as
_
string(
0
))
s.sendmail(sender, recips, outer.as
_
string())
s.close()
s.close()
...
@@ -556,7 +551,7 @@ def main():
...
@@ -556,7 +551,7 @@ def main():
counter = 1
counter = 1
for part in msg.walk():
for part in msg.walk():
# multipart/* are just containers
# multipart/* are just containers
if part.get
_
main
_
type() == 'multipart':
if part.get
_
content
_
main
type() == 'multipart':
continue
continue
# Applications should really sanitize the given filename so that an
# Applications should really sanitize the given filename so that an
# email message can't be used to overwrite important files
# 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