0

I'm getting a "aborted (disconnected)" when sending multiple emails with smtplib (python 3.6) and I can't find much information online. I'm pretty sure the error is coming from the server and not my code but I'd like to know how to fix this or at least get a clue of what to google to get more information because I am clueless as to how I am going to fix this.

This is the function I'm using the send one e-mail: ( I am calling this function five times and I'm getting the error ).

import smtplib

def enviar_um_mail(to, subject, body):
    #source: https://stackabuse.com/how-to-send-emails-with-gmail-using-python/
    user = '<mail here>'
    password = '<passwrd here>' 

    sent_from = user
    to = 'somemail@somedomain.org'
    email_text = 'text of e-mail here'

    try:
        server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
        server.ehlo()
        server.login(user, password)
        server.sendmail(sent_from, to, email_text)
        server.close()

        print ('Email sent!')
    except Exception as e:
        print ('Something went wrong...')
        print(e)

When calling this function five times the python shell shows:

Email sent!
Email sent!
Email sent!
Email sent!
Email sent!aborted (disconnected)
  • if you are sending from the same email account, try not to open and close a session every time, and see if it fixes – xiaxio Jan 25 '20 at 15:26
  • this did not fix it. I also tried with server.quit ( also not opening and closing the connection multiple times ) and it didn't work either – GigiNicaragua Jan 25 '20 at 15:42
  • looks like an email server restriction. Try with other server (gmail?) and see if it works. If this is the problem, you would have to contact (pay?) in your email server service – xiaxio Jan 25 '20 at 15:46
  • I just ran the code in my computer, using gmail server and it works fine, with a loop sending 10 continuous emails. I don't see what else could be happening. Sorry I can't be of more help... – xiaxio Jan 25 '20 at 16:22
  • No problem, you're awesome! – GigiNicaragua Jan 28 '20 at 00:43
  • @xiaxio magically, it fixed itself. All I did was copy the code I wrote here to the place where I took it from. The only thing I see that could have been creating the issue was that I was doing "import time" at the beggining of the script but never really used time's functions. I which I could add more info to this problem for anyone that encounters it in the future but I can't. Thanks for the help and have a great week. – GigiNicaragua Feb 01 '20 at 05:40
  • I'm glad it got fixed. Enjoy coding! – xiaxio Feb 01 '20 at 10:44

0 Answers0