0

I have checked over the code and made sure that my credentials are correct but to no avail. I keep getting an error that looks like this:

smtplib.SMTPServerDisconnected: Connection unexpectedly closed

Any help would be appreciated :)

Here's the code:

import smtplib


def EmailSender():
    ##try:
    server=smtplib.SMTP(host='smtp.gmail.com', port=465)
    server.starttls()
    server.login("my gmail email","my gmail password")

    msg=MIMEMultipart()
    message=message_template.substitute(NAME=name.title())
    msg['From']=MYADDR
    msg['To']=SENDTO
    msg['Subject']="Testing functionality of Python is sometimes painstaking, but when you see that message come through, it's well worth it :)"

    msg.attatch(MIMEText(message, 'plain'))

    server.send_message(msg)
    ##except:
    ##    print("There was an error esablishing a connection...")

EmailSender()
Mimakari
  • 411
  • 3
  • 12
  • You probably want port 587: see https://support.google.com/a/answer/176600?hl=en . Scroll down to "Use the Gmail SMTP Server". If you have 2FA on your Google account (and if not, you should!), you'll need to make an App Password for that that's different from your normal Gmail password. – Kirk Strauser Jan 30 '20 at 18:02
  • The duplicate I nominated is a grab bag of common problems. If you still need help, please [edit] to provide the exact traceback and ideally a trace from running with debugging enabled. – tripleee Jan 30 '20 at 18:10
  • A multipart with only a single attachment is slightly weird; try with a well-formed single-part message with an actual body. – tripleee Jan 30 '20 at 18:13

0 Answers0