mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
#24277: The new email API is no longer provisional.
This is a wholesale reorganization and editing of the email documentation to make the new API the standard one, and the old API the 'legacy' one. The default is still the compat32 policy, for backward compatibility. We will change that eventually.
This commit is contained in:
parent
23e8633781
commit
29d1bc0842
25 changed files with 2078 additions and 1670 deletions
70
Doc/includes/email-alternative.py
Executable file → Normal file
70
Doc/includes/email-alternative.py
Executable file → Normal file
|
@ -2,47 +2,55 @@
|
|||
|
||||
import smtplib
|
||||
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
from email.message import EmailMessage
|
||||
from email.headerregistry import Address
|
||||
from email.utils import make_msgid
|
||||
|
||||
# me == my email address
|
||||
# you == recipient's email address
|
||||
me = "my@email.com"
|
||||
you = "your@email.com"
|
||||
# Create the base text message.
|
||||
msg = EmailMessage()
|
||||
msg['Subject'] = "Ayons asperges pour le déjeuner"
|
||||
msg['From'] = Address("Pepé Le Pew", "pepe", "example.com")
|
||||
msg['To'] = (Address("Penelope Pussycat", "penelope", "example.com"),
|
||||
Address("Fabrette Pussycat", "fabrette", "example.com"))
|
||||
msg.set_content("""\
|
||||
Salut!
|
||||
|
||||
# Create message container - the correct MIME type is multipart/alternative.
|
||||
msg = MIMEMultipart('alternative')
|
||||
msg['Subject'] = "Link"
|
||||
msg['From'] = me
|
||||
msg['To'] = you
|
||||
Cela ressemble à un excellent recipie[1] déjeuner.
|
||||
|
||||
# Create the body of the message (a plain-text and an HTML version).
|
||||
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttps://www.python.org"
|
||||
html = """\
|
||||
[1] http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718
|
||||
|
||||
--Pepé
|
||||
""")
|
||||
|
||||
# Add the html version. This converts the message into a multipart/alternative
|
||||
# container, with the original text message as the first part and the new html
|
||||
# message as the second part.
|
||||
asparagus_cid = make_msgid()
|
||||
msg.add_alternative("""\
|
||||
<html>
|
||||
<head></head>
|
||||
<body>
|
||||
<p>Hi!<br>
|
||||
How are you?<br>
|
||||
Here is the <a href="https://www.python.org">link</a> you wanted.
|
||||
<p>Salut!<\p>
|
||||
<p>Cela ressemble à un excellent
|
||||
<a href="http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718>
|
||||
recipie
|
||||
</a> déjeuner.
|
||||
</p>
|
||||
<img src="cid:{asparagus_cid}" \>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
""".format(asparagus_cid=asparagus_cid[1:-1]), subtype='html')
|
||||
# note that we needed to peel the <> off the msgid for use in the html.
|
||||
|
||||
# Record the MIME types of both parts - text/plain and text/html.
|
||||
part1 = MIMEText(text, 'plain')
|
||||
part2 = MIMEText(html, 'html')
|
||||
# Now add the related image to the html part.
|
||||
with open("roasted-asparagus.jpg", 'rb') as img:
|
||||
msg.get_payload()[1].add_related(img.read(), 'image', 'jpeg',
|
||||
cid=asparagus_cid)
|
||||
|
||||
# Attach parts into message container.
|
||||
# According to RFC 2046, the last part of a multipart message, in this case
|
||||
# the HTML message, is best and preferred.
|
||||
msg.attach(part1)
|
||||
msg.attach(part2)
|
||||
# Make a local copy of what we are going to send.
|
||||
with open('outgoing.msg', 'wb') as f:
|
||||
f.write(bytes(msg))
|
||||
|
||||
# Send the message via local SMTP server.
|
||||
s = smtplib.SMTP('localhost')
|
||||
# sendmail function takes 3 arguments: sender's address, recipient's address
|
||||
# and message to send - here it is sent as one string.
|
||||
s.sendmail(me, you, msg.as_string())
|
||||
s.quit()
|
||||
with smtplib.SMTP('localhost') as s:
|
||||
s.send_message(msg)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue