Error with sending email massage

Hi Everyone,

Please I need help with email sending. I have followed the codes as Mosh explained in the course video but it is not working. It raised ‘typeerror’ on line 7, that object does not support item assignment. Please see what corrections are required in this script.

Please I need help as quickly as possible. I have been stuck here for days now and cannot progress for now

At a glance, you seem to be missing () after MIMEMultipart on line 6, if you can put your code directly into a reply for me to look at I can check it a bit more thoroughly. You can do this by copy-pasting it into the comment with ``` above and below the code. The ` key is to the left of the 1 key.

For example, here is how my notes look from that section:

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from pathlib import Path
import smtplib


message = MIMEMultipart()
message["from"] = "Test User"
message["to"] = "notarealemail@gmail.com"
message["subject"] = "This is a test"
message.attach(MIMEText("Body"))
message.attach(MIMEImage(Path("mosh.png").read_bytes))

with smtplib.SMTP(host="smtp.gmail.com", port=587) as smtp:
    smtp.ehlo()
    smtp.starttls()
    smtp.login("notarealemail@gmail.com", "thisiswhereapasswordgoes")
    smtp.sendmessage(message)
    print("Sent...")

Lastly, you are going to want to edit this post to remove your login credentials, and likely change your password for good measure if you have it tied to any other logins that are important to you. Just in case.

1 Like

Thanks @MHarten for your response. I have implemented your first observation, the missing () after MIMEMultipart, the error now is “smtplib.SMTPConnectError:” from line 12.
Meanwhile the email and password in this post are not the real ones.

Below is the editable version of my code:

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
import ssl

message = MIMEMultipart()
message[“from”] = “Mose Someone”
message[“to”] = “someone@gmail.com
message[“subject”] = “This is test mail”
message.attach(MIMEText(“Body of Message”))

with smtplib.SMTP(host=“smtp.gmail.com”, port=587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.login(“someone@gmail.com”, “newpasskey+90”)
smtp.send_message(message)
print(“sent…”)

The code still cannot run. Below is the new error message:

Traceback (most recent call last):
File “c:/…/Helloworld/e_mailling.py”,
line 12, in
with smtplib.SMTP(host=“smtp.gmail.com”, port=587) as smtp: File “C:.…\Python38-32\lib\smtplib.py”, line 256, in init
raise SMTPConnectError(code, msg)
smtplib.SMTPConnectError: (421, b’Service not available’)