磁盘空间不足时,smtplib.send_message失败

时间:2020-06-18 12:41:41

标签: python smtp

我有大量数据和小磁盘。脚本的结果将通过电子邮件发送到收件人列表。这需要包括任何错误。

try:
    ...
# Most likely out of space
except OSError as e:
    notify(args.e, Action.UNRECOVERABLE)
def notify(recipients: str, action: Action, body: str = None) -> None:
    """
    Sends an email with the result of the script.

    :param recipients: Comma-delimited list of email address
    :param action: Result of the script
    :param body: Content of the email
    """
    if body is None:
        body = action.value
    msg = email.message.EmailMessage()
    msg.set_content(body)
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = recipients
    s = smtplib.SMTP('localhost')
    s.send_message(msg)
    s.quit()

但是,当空间不足时,会出现此错误:

Traceback (most recent call last):
    ....
OSError: [Errno 28] No space left on device

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File <filename>, line 257, in <module>
    main()
  File <filename>, line 254, in main
    notify(args.e, Action.UNRECOVERABLE)
  File <filename>, line 30, in notify
    s.send_message(msg)
  File "/usr/lib64/python3.6/smtplib.py", line 967, in send_message
    rcpt_options)
  File "/usr/lib64/python3.6/smtplib.py", line 882, in sendmail
    (code, resp) = self.data(msg)
  File "/usr/lib64/python3.6/smtplib.py", line 569, in data
    (code, msg) = self.getreply()
  File "/usr/lib64/python3.6/smtplib.py", line 394, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

简单的解决方案可能是删除我们刚刚创建的文件,但这并不是最佳选择。

0 个答案:

没有答案