Python-符合条件后未发送电子邮件

时间:2019-05-13 19:07:31

标签: python-3.x sendmail

我正在尝试使用Python3.7编写URL检查程序。如果列表中的URL之一为DOWN,则可以接收电子邮件,但是即使URL为UP,也无法接收电子邮件,即使我使用了以下代码也是如此。你能建议我想念什么吗?

    if status != "200":
        server.sendmail(sender_email, receiver_email, message)
    elif status == "200":
        server.sendmail(sender_email, receiver_email, message_up)
def checkUrls():
    for url in URLS:
        status = "N/A"
        try:
            status = checkUrl(url)
        except requests.exceptions.ConnectionError:
            status = "DOWN"

        printStatus(url, status)

    context = ssl.create_default_context()
    with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
        server.login(sender_email, password)

        if status != "200":
            server.sendmail(sender_email, receiver_email, message)
        elif status == "200":
            server.sendmail(sender_email, receiver_email, message_up)

def checkUrl(url):
    r = requests.get(url,timeout=5)
    #print r.status_code
    return str(r.status_code)

def printStatus(url, status):
    color = GREEN
    print("The " +url+" is "+ status)     
    print ((color + ' ' + status+ ' ' + ENDC + ' ') + url)

0 个答案:

没有答案