简体   繁体   中英

sending a simple email with python3

I am trying to send an email on python3. I am forwarding the email to a relay server. I have followed several tutorials online and all give me the same error message. Not sure how to fix it.

Traceback (most recent call last):
  File "testEmail.py", line 17, in <module>
    msg['Subject'] = "Test mail"
TypeError: 'str' object does not support item assignment
#!/usr/bin/python3

import smtplib

port = 25
msg = "This is test mail"

msg['Subject'] = "Test mail"
msg['From'] = 'test@test.com'
msg['To'] = 'my-name@test2.com'

s = smtplib.SMTP('10.1.2.3', port)
s.send_messsage(msg)
s.quit()
print("Successfully sent email")

According to all the examples in the documentation , msg needs to be an EmailMessage .

msg = EmailMessage()
msg.set_content("This is test mail")
msg['Subject'] = "Test mail"
...

It's good to just read the basic, official documentation and examples when using a module so you don't miss basic stuff like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM