mirror of
https://github.com/python/cpython
synced 2024-09-04 15:56:13 +00:00
![Jean-Baptiste Poupon](/assets/img/avatar_default.png)
* Fix the french used in the email documentation The french used in one of the example was either machine translated a while ago or written by someone who does not speak french. Fixed it by using grammatically correct french.
57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import smtplib
|
|
|
|
from email.message import EmailMessage
|
|
from email.headerregistry import Address
|
|
from email.utils import make_msgid
|
|
|
|
# Create the base text message.
|
|
msg = EmailMessage()
|
|
msg['Subject'] = "Pourquoi pas des asperges pour ce midi ?"
|
|
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!
|
|
|
|
Cette recette [1] sera sûrement un très bon repas.
|
|
|
|
[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>Salut!</p>
|
|
<p>Cette
|
|
<a href="http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718">
|
|
recette
|
|
</a> sera sûrement un très bon repas.
|
|
</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.
|
|
|
|
# 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)
|
|
|
|
# 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.
|
|
with smtplib.SMTP('localhost') as s:
|
|
s.send_message(msg)
|