Python发送邮件在异常中执行脚本实现死循环

本文详细介绍了使用Python通过SMTP服务发送邮件的过程,并演示了如何在遇到异常时进行邮件通知,确保程序的稳定运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#-*- encoding: utf-8 -*-
'''
try.py
Created on 2019/1/20 16:48
Copyright (c) 2019/1/20, IT_BULL版权所有.
@author: IT_BULL
'''

import time
import smtplib
from email.header import Header
from email.mime.text import MIMEText

import os



# 第三方 SMTP 服务
host = "smtp.163.com"      # SMTP服务器
port = 465
user = "it_bull@163.com"                  # 用户名
passWD = "18088296721wy"               # 授权密码,非登录密码

sender = 'it_bull@163.com' #发邮件人
receivers = 'it_bull@163.com,243961515@qq.com' #收邮件人

content = '''
    快来看啊,亲爱的小爬虫被杀死啦!
''' #邮件内容

subject = '亲爱的小爬虫被杀死啦'  # 邮件主题


def send_email(host, user, passWD, receivers, subject, content):
    email_client = smtplib.SMTP(host)
    email_client.login(user, passWD)
    # create msg
    msg = MIMEText(content+'\n'+"北京时间:"+time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), 'plain', 'utf-8')
    msg['From'] = user
    msg['To'] = receivers
    msg['Subject'] = Header(subject+' send_email2', 'utf-8')

    email_client.sendmail(user, receivers, msg.as_string())

    email_client.quit()
    # print(msg)
    print('邮件2发送成功!')


    # send_email(host, user, passWD, receivers, subject, content)

def test():
    for i in range(1000):
        print(i)
        if(i == 5):
            f = open("aa.txt")


try:
    test()
except Exception as e:
    print("薄荷味==============")
    print(e)
    time.sleep(50)
    str = ('python try.py')
    p = os.system(str)
    print(p)

finally:
    subject = "打不死的小强,不死神功"
    send_email(host, user, passWD, receivers, subject, content)

 

转载于:https://www.cnblogs.com/itBulls/articles/10295780.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值