在python中发送电子邮件 - Google是否更改了有关gmail的内容?

时间:2012-07-18 15:41:48

标签: python

我有一直用于发送电子邮件的基本工具 我使用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..."

0 个答案:

没有答案