This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add simple usage to email module
Type: enhancement Stage:
Components: Documentation Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, eric.smith, tarao1006
Priority: normal Keywords:

Created on 2021-12-06 10:44 by tarao1006, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg407803 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-12-06 13:30
What sort of usage example would help you?

Is https://docs.python.org/3/library/email.examples.html lacking something?
msg407839 - (view) Author: Taiga Katarao (tarao1006) * Date: 2021-12-06 16:51
There are the simplest example to send text email and a little bit complicated example to an email with some files.

However, beginners like me want simple example to create an email composed of the combination of multipart/alternative and multipart/mixed.

There are many web sites to explain sending an email composed of multipart/alternative and multipart/mixed, but all of them use MIMEText and MIMEMultipart, which can be replaced and simplified with EmailMessage like below.

```
import smtplib
from email.message import EmailMessage

msg = EmailMessage()
msg['From'] = 'from@example.com'
msg['To'] = 'to@example.com'
msg['Subject'] = 'Subject'

msg.add_alternative('Hello, world.', subtype='text')
msg.add_alternative('<h1>Helo, world.</h1>', subtype='html')
with open('example.pdf', 'rb') as f:
    msg.add_attachment(
        f.read(),
        maintype='application',
        subtype='pdf',
        filename='example.pdf'
    )

with smtplib.SMTP('SMTP_HOST', 'SMTP_PORT') as smtp:
    smtp.starttls()
    smtp.login('USER', 'PASSWORD')
    smtp.send_message(msg)
```

Of cause I know we can obtain the code above from the combination of existing example codes, but it's a little bit difficult.

I guess the reason why they cannot find simple way partially because the official documentation does not provide example.
History
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90152
2021-12-06 16:51:58tarao1006setmessages: + msg407839
2021-12-06 13:30:53eric.smithsetnosy: + eric.smith
messages: + msg407803
2021-12-06 10:44:20tarao1006create