문제 설명
예외 경우에 인쇄되는 오류 메시지를 사용자에게 이메일로 어떻게 보내나요? (How to send an error message being printed in an exception case by email to a user?)
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()
다음은 Python 셸에 표시되는 오류입니다.
ERROR MESSAGE [Errno 2] No such file or directory: 'C:\\Python37\\Processed\\Invoice.xlsx'
'e' 변수에 인쇄되는 오류 메시지를 이메일로 다른 사람에게 보내는 방법
감사합니다
참조 솔루션
방법 1:
The logging module itself has smtp handler you can do something like this:
import logging
import logging.handlers
smtp_handler = logging.handlers.SMTPHandler(mailhost=("smtp.example.com", 25),
fromaddr="someone@something.com",
toaddrs="receiver@mail.com",
subject=u"ERROR IN YOURAPP!")
logger = logging.getLogger()
logger.addHandler(smtp_handler)
try:
raise Exception
except Exception as e:
logger.exception('Unhandled Exception')
for more info see doc
(by Gavya Mehta、Eternal)