0

私はPython3.7を使用しています。コードが機能していない場合、例外条件のエラーをユーザーにメールで送信しようとしています。次のコードは次のとおりです。

except Exception as e:
logging.basicConfig(filename='logfile.log',format='%(asctime)s %(message)s',filemode='w')

logger=logging.getLogger()

logger.setLevel(logging.ERROR)

logger.error(e)
print("ERROR MESSAGE",e)

MY_ADDRESS = '*****@gmail.com'
PASSWORD = '*******'
MY_ADDRESS1 = '****@gmail.com'

s = smtplib.SMTP(host='smtp.gmail.com', port=***)
s.starttls()
s.login(MY_ADDRESS, PASSWORD)
print("login")
msg = MIMEMultipart()       # create a message

msg['From']=MY_ADDRESS
msg['To']=MY_ADDRESS1
msg['Subject']="ERROR MESSAGE"

message="ERROR"
msg.attach.as_string(MIMEText(e))
print("ERROR MAILED")

s.send_message(msg)
s.quit()

出力されている変数「e」でエラーメッセージを送信したい

しかし、私はそうすることができません。以下はエラーです:

これは私が印刷してメールしたいエラーです:

ERROR MESSAGE [Errno 2] No such file or directory: 'C:\\Python37\\Processed\\Invoice.xlsx'

これは、python シェルに表示されるエラーです。

Traceback (most recent call last):
  File "C:\Python37\Sopan.py", line 30, in <module>
    wb.save(workbook_name)
  File "C:\Python37\lib\site-packages\openpyxl\workbook\workbook.py", line 408, in save
    save_workbook(self, filename)
  File "C:\Python37\lib\site-packages\openpyxl\writer\excel.py", line 291, in save_workbook
    archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
  File "C:\Python37\lib\zipfile.py", line 1207, in __init__
    self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Python37\\Processed\\Invoice.xlsx'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python37\Sopan.py", line 88, in <module>
    msg.attach.as_string(MIMEText(e))
AttributeError: 'function' object has no attribute 'as_string'
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python37\Sopan.py", line 88, in <module>
    msg.attach.as_string(MIMEText(e))
AttributeError: 'function' object has no attribute 'as_string'

変数 'e' に出力されているエラー メッセージを電子メールで誰かに送信するにはどうすればよいですか?

ありがとうございました

4

1 に答える 1