Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 2 | |
| 3 | import smtplib |
| 4 | |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 5 | from email.message import EmailMessage |
| 6 | from email.headerregistry import Address |
| 7 | from email.utils import make_msgid |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 8 | |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 9 | # Create the base text message. |
| 10 | msg = EmailMessage() |
| 11 | msg['Subject'] = "Ayons asperges pour le déjeuner" |
| 12 | msg['From'] = Address("Pepé Le Pew", "pepe", "example.com") |
| 13 | msg['To'] = (Address("Penelope Pussycat", "penelope", "example.com"), |
| 14 | Address("Fabrette Pussycat", "fabrette", "example.com")) |
| 15 | msg.set_content("""\ |
| 16 | Salut! |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 17 | |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 18 | Cela ressemble à un excellent recipie[1] déjeuner. |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 19 | |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 20 | [1] http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718 |
| 21 | |
| 22 | --Pepé |
| 23 | """) |
| 24 | |
| 25 | # Add the html version. This converts the message into a multipart/alternative |
| 26 | # container, with the original text message as the first part and the new html |
| 27 | # message as the second part. |
| 28 | asparagus_cid = make_msgid() |
| 29 | msg.add_alternative("""\ |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 30 | <html> |
| 31 | <head></head> |
| 32 | <body> |
R David Murray | 1ba3e6d | 2016-09-07 21:48:21 -0400 | [diff] [blame] | 33 | <p>Salut!</p> |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 34 | <p>Cela ressemble à un excellent |
Henk-Jaap Wagenaar | 8337239 | 2017-09-24 17:12:53 +0100 | [diff] [blame] | 35 | <a href="http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718"> |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 36 | recipie |
| 37 | </a> déjeuner. |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 38 | </p> |
R David Murray | 1ba3e6d | 2016-09-07 21:48:21 -0400 | [diff] [blame] | 39 | <img src="cid:{asparagus_cid}" /> |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 40 | </body> |
| 41 | </html> |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 42 | """.format(asparagus_cid=asparagus_cid[1:-1]), subtype='html') |
| 43 | # note that we needed to peel the <> off the msgid for use in the html. |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 44 | |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 45 | # Now add the related image to the html part. |
| 46 | with open("roasted-asparagus.jpg", 'rb') as img: |
| 47 | msg.get_payload()[1].add_related(img.read(), 'image', 'jpeg', |
| 48 | cid=asparagus_cid) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 49 | |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 50 | # Make a local copy of what we are going to send. |
| 51 | with open('outgoing.msg', 'wb') as f: |
| 52 | f.write(bytes(msg)) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 53 | |
| 54 | # Send the message via local SMTP server. |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 55 | with smtplib.SMTP('localhost') as s: |
| 56 | s.send_message(msg) |