smtplib 邮件模块

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from live.settings import SEND_HOUR_TIME, SEND_MINUTE_TIME, T_SEND_HOUR_TIME, T_SEND_MINUTE_TIME

username = 'kkkse@163.com'
password = 'SPICGNRLY'
sender = username
receivers = ','.join(['sjdfo@163.net'])  # 接收者
# receivers = ','.join(['nnnn@163.com'])  # 接收者

from apscheduler.schedulers.blocking import BlockingScheduler
sc = BlockingScheduler()

@sc.scheduled_job('cron', day_of_week='*', hour=SEND_HOUR_TIME, minute=f'{SEND_MINUTE_TIME}', second='00')
def run():
    msg = MIMEMultipart()
    msg['Subject'] = '主题'
    msg['From'] = sender
    msg['To'] = receivers

    puretext = MIMEText('')
    msg.attach(puretext)

    xlsxpart = MIMEApplication(open('data.xlsx', 'rb').read())
    xlsxpart.add_header('Content-Disposition', 'attachment', filename='data.xlsx')
    msg.attach(xlsxpart)

    client = smtplib.SMTP()
    client.connect('smtp.163.com')
    client.login(username, password)
    client.sendmail(sender, receivers, msg.as_string())
    client.quit()
    print('邮件发送成功!')


sd = BlockingScheduler()

@sd.scheduled_job('cron', day_of_week='*', hour=SEND_HOUR_TIME, minute=f'{SEND_MINUTE_TIME}', second='00')
def run():
    msg = MIMEMultipart()
    msg['Subject'] = '主题'
    msg['From'] = sender
    msg['To'] = receivers

    puretext = MIMEText('')
    msg.attach(puretext)

    xlsxpart = MIMEApplication(open('data1.xlsx', 'rb').read())
    xlsxpart.add_header('Content-Disposition', 'attachment', filename='data1.xlsx')
    msg.attach(xlsxpart)

    client = smtplib.SMTP()
    client.connect('smtp.163.com')
    client.login(username, password)
    client.sendmail(sender, receivers, msg.as_string())
    client.quit()
    print('邮件发送成功!')


send_email1 = sc.start
send_email2 = sd.start


定时发送

from live.test import start1, start2

import threading

def main():
    t1 = threading.Thread(target=start1)		# 函数名,不带括号
    t2 = threading.Thread(target=start2)
    t1.start()
    t2.start()

if __name__ == '__main__':
    main()

原文地址:https://www.cnblogs.com/kai-/p/13858372.html