1

我有一些我一直用来发送电子邮件的基本实用程序。
我使用 gmail 发送警报、通知等...

现在,他们都在解决这个错误,我想知道是不是因为谷歌。

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

下面是我通常使用的示例代码。我什至不记得我上次修改代码是什么时候了。

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..."
4

0 回答 0