1

I have basic utilities that I have always used for sending emails.
I use gmail to send alerts, notifications etc...

Now, they are ALL breaking with this error and I am wondering if its because of google.

Traceback (most recent call last):
  File "/home/ubuntu/workspace/includes/utility.py", line 44, in <module>
    sendMail('cctest',parms,body='ok - no cc')
  File "/home/ubuntu/workspace/includes/utility.py", line 38, in sendMail
    server.sendmail(fromaddr, toaddrs, message)
  File "/usr/lib/python2.7/smtplib.py", line 700, in sendmail
    self.rset()
  File "/usr/lib/python2.7/smtplib.py", line 441, in rset
    return self.docmd("rset")
  File "/usr/lib/python2.7/smtplib.py", line 366, in docmd
    return self.getreply()
  File "/usr/lib/python2.7/smtplib.py", line 343, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

Below is a sample code I typically use. I cant even remember the last time I modified the code.

def sendMail(subject,parms,body=None,cc=None):

    import smtplib
    import email.utils
    from email.mime.text import MIMEText
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEBase import MIMEBase
    from email import Encoders

    toaddr = parms['email_to']
    fromaddr = parms['email_from']
    message_subject = subject
    message_text = body
    if cc:
        message = "From: %s\r\n" % fromaddr + "To: %s\r\n" % toaddr + "CC: %s\r\n" % ",".join(cc) + "Subject: %s\r\n" % message_subject + "\r\n" + message_text
        toaddrs = [toaddr] + cc #+ bcc
    else:
        message = "From: %s\r\n" % fromaddr + "To: %s\r\n" % toaddr + "Subject: %s\r\n" % message_subject + "\r\n" + message_text
        toaddrs = [toaddr]

    server = smtplib.SMTP('smtp.gmail.com',587)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(fromaddr,parms['email_password'])
    server.sendmail(fromaddr, toaddrs, message)
    server.quit()

    print "Email sent..."
Tampa
  • 75,446
  • 119
  • 278
  • 425
  • Run "telnet smtp.gmail.com 587" command to check primarily the connection from your end is hitting the mail server or not. – dan-boa Jul 18 '12 at 15:47
  • ran it. it worked. Trying 173.194.79.108... Connected to gmail-smtp-msa.l.google.com. Escape character is '^]'. 220 mx.google.com ESMTP pi7sm16419155pbb.56 – Tampa Jul 18 '12 at 15:50
  • It implies that their is no connectivity issue. Please refer to the code mentioned in the link. Its very similar to your code and you can determine what is going wrong. http://stackoverflow.com/questions/6367014/how-to-send-email-via-django – dan-boa Jul 18 '12 at 16:12
  • Well...I just changed the email address. I was sending to my self. When I used a different email. It worked. Again, I dont know if I reported my self as spam...haha...or what. – Tampa Jul 19 '12 at 14:41

0 Answers0