0

I can understand that with the syntax of a['x'] = y, you could add a new key:value into a dictionary. But, when I was learning how could I send a email with the smtplib, something made me confused. The code as following:

import smtplib
from email.mime.text import MIMEText

msg = MIMEText('test','plain','utf-8')
msg['From'] = 'test'

print(msg,type(msg))

And I got a result like:

Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
From: test

dGVzdA==
 <class 'email.mime.text.MIMEText'>

It makes my confused that 'msg['From'] = 'test'' here seems to add a parameter or something to the instance of class 'email.mime.text.MIMETex' which name is 'msg', is that right?

Please someone could show my an example that could help clarify the usage? Is there any advantage or benefit with this usage?

Thanks in advance.

  • The relevant documentation is here: https://docs.python.org/3/library/email.compat32-message.html#email.message.Message.__setitem__ – deceze May 20 '20 at 08:22
  • 1
    Working with key:value pairs is not limited to dictionaries. – VPfB May 20 '20 at 08:26

0 Answers0