2013-04-20 2 views
0

내가 인해 메일 서버 문제로 가끔 파이썬 플라스크 mail.send 오류 검사 및 로그가

if app.config['MAIL']: 
     mail.send(message) 
    else: 
     print message.html 

같은 메일 코드 부분 플라스크 응용 프로그램을 가지고는 mail.send() 함수가 실패합니다. 오류 상태 을 확인하고 어떻게 로그합니까?

if app.config['MAIL']: 
    try: 
     mail.send(message) 
    except SMTPException, e: 
     current_app.logger.error(e.message) 
else: 
    print message.html 

당신은 SMTPException에 따라 많은 예외를 찾을 수 있습니다 :

에서는 예외가 잡으려고

 if app.config['MAIL']: 
     retcode=mail.send(message) 
     else: 
     print message.html 
     # now log it 
     if (retcode != 0): 
     #log it or take anyother action. 

답변

4

시도처럼 뭔가를 할 http://docs.python.org/2/library/smtplib.html#smtplib.SMTPException. 당신이 진짜 코드를 반환해야하는 경우

당신은 같은 것을 할 수 있습니다 응답에 대한

retcode = 0 
if app.config['MAIL']: 
    try: 
     mail.send(message) 
    except SMTPAuthenticationError, e: 
     retcode = 2 
    except SMTPServerDisconnected, e: 
     retcode = 3 
    except SMTPException, e: 
     retcode = 1 
else: 
    print message.html 

if retcode: 
    current_app.logger.error(retcode) 
+0

감사합니다! –