吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1239|回复: 42
收起左侧

[Python 原创] 定时检测公历农历重要节日与朋友生日并提前或者当天发送邮件提醒

  [复制链接]
你怎么长不高啊 发表于 2024-3-27 09:39
本帖最后由 你怎么长不高啊 于 2024-3-29 18:30 编辑

由于要记得朋友生日和重要节日太多,很容易忘记,于是写一个python程序每天定时检测并提前提醒我做节日准备,以下是源代码可自行修改
如果对你有用请留下评分谢谢!
[Python] 纯文本查看 复制代码
# 导入必要的模块
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
from lunardate import LunarDate
import datetime
import schedule
import time

# 定义朋友的农历生日信息和你的电子邮件地址
birthdays = {
    '朋友1': {'lunar_birthday': LunarDate(1998, 11, 21), 'email': 'your_email@example.com'},
    '朋友2': {'lunar_birthday': LunarDate(1999, 4, 9), 'email': 'your_email@example.com'},

    # 继续添加更多朋友
}
# 定义重要节日信息和你的电子邮件地址,date代表公历,lunar_date代表农历
festivals = {
  '节日1': {'date': datetime.date(2024, 3, 27), 'email': 'your_email@example.com'},
  '节日2': {'lunar_date': LunarDate(1992, 2, 25), 'email': 'your_email@example.com'},

  # 继续添加更多节日
}

# SMTP服务器信息
smtp_server = 'smtp.qq.com'

# smtp服务器的用户名和密码,注意密码要填POP3/IMAP/SMTP服务得授权码
smtp_user = 'your_email@example.com'
smtp_pass = 'your_password'

# 发送邮件的函数
def send_email(receiver_email, subject, content):
    # 创建一个邮件消息
    msg = MIMEText(content, 'plain', 'utf-8')
    msg['From'] = formataddr(['Birthday Reminder', smtp_user])
    msg['To'] = formataddr(['Dear User', receiver_email])
    msg['Subject'] = subject

    try:
        # 与SMTP服务器建立一个 SSL 链接
        server = smtplib.SMTP_SSL(smtp_server)
        # 用SMTP服务器的用户名和密码登录
        server.login(smtp_user, smtp_pass)
        # 发送邮件
        server.sendmail(smtp_user, [receiver_email], msg.as_string())
        server.quit()
        print('邮件发送成功')
    except smtplib.SMTPException as e:
        print('邮件发送失败', e)

def check_and_send_birthday_emails():
    now = datetime.datetime.now()
    today = LunarDate.fromSolarDate(now.year, now.month, now.day)
    # 获取公历和农历的今天日期
    today_solar = datetime.date.today()
    today_lunar = LunarDate.fromSolarDate(today_solar.year, today_solar.month, today_solar.day)


    # 遍历每一个朋友生日信息提前7天提醒
    for friend, info in birthdays.items():
        # 如果7天后是他的农历生日,则发送邮件
        in_a_week = today + datetime.timedelta(days=7)
        if info['lunar_birthday'].month == in_a_week.month and info['lunar_birthday'].day == in_a_week.day:
            subject = f'7天后是 {friend} 的农历生日!'
            content = f'提前准备给 {friend} 农历生日的祝福吧!'
            send_email(info['email'], subject, content)

    # 遍历每一个朋友生日信息提前3天提醒
    for friend, info in birthdays.items():
        # 如果3天后是他的农历生日,则发送邮件
        in_a_week = today + datetime.timedelta(days=3)
        if info['lunar_birthday'].month == in_a_week.month and info['lunar_birthday'].day == in_a_week.day:
            subject = f'3天后是 {friend} 的农历生日!'
            content = f'提前准备给 {friend} 农历生日的祝福吧!'
            send_email(info['email'], subject, content)

    # 遍历每一个朋友生日信息当天再次提醒
    for friend, info in birthdays.items():
        # 如果今天是他的农历生日,则发送邮件
        if info['lunar_birthday'].month == today.month and info['lunar_birthday'].day == today.day:
            subject = f'今天是 {friend} 的农历生日!'
            content = f'快给 {friend} 发去农历生日的祝福吧!'
            send_email(info['email'], subject, content)

        # 遍历每一个重要节日公历提前7天提醒
    for festival, info in festivals.items():
        in_a_week = now.date() + datetime.timedelta(days=7)
        in_a_week_lunar = LunarDate.fromSolarDate(in_a_week.year, in_a_week.month, in_a_week.day)

        #检查公历节日
        if 'date' in info and info['date'].replace(year=in_a_week.year) == in_a_week:
            subject = f'7天后是 {festival}!'
            content = f'提前准备好庆祝 {festival} 吧!'
            send_email(info['email'], subject, content)

        # 检查农历节日
        if 'lunar_date' in info and info['lunar_date'].month == in_a_week_lunar.month and info[
            'lunar_date'].day == in_a_week_lunar.day:
            subject = f'7天后是 {festival}!'
            content = f'提前准备好庆祝 {festival} 吧!'
            send_email(info['email'], subject, content)
            # print("7天后的农历日期是:", in_a_week_lunar)

        # 遍历每一个重要节日当天提醒
    for festival, info in festivals.items():
        # 检查公历节日
        if 'date' in info and info['date'].replace(year=today_solar.year) == today_solar:
            subject = f'今天是 {festival}!'
            content = f'快去庆祝 {festival} 吧!'
            send_email(info['email'], subject, content)

        # 检查农历节日
        if 'lunar_date' in info and info['lunar_date'].month == today_lunar.month and info[
            'lunar_date'].day == today_lunar.day:
            subject = f'今天是 {festival}!'
            content = f'快去庆祝 {festival} 吧!'
            send_email(info['email'], subject, content)


# 运行一次测试
#check_and_send_birthday_emails()

# 定义一个函数用来检查并执行所有计划任务
def run_pending_tasks():
    while True:
        schedule.run_pending()
        time.sleep(1)

# 设定调度任务:每天的0点运行 check_and_send_birthday_emails 函数
schedule.every().day.at("00:00:00").do(check_and_send_birthday_emails)

# 开始运行调度任务
run_pending_tasks()

免费评分

参与人数 6吾爱币 +8 热心值 +5 收起 理由
thefresher + 1 谢谢@Thanks!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
dandna + 1 用心讨论,共获提升!
Kuangsi + 1 用心讨论,共获提升!
321cdexswzaQ + 1 我很赞同!
落木萧萧 + 1 热心回复!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

caiop 发表于 2024-3-27 15:16
用楼主的代码改了个企业微信推送的,结果一直运行没反应没结果,不知道啥情况,还得再研究研究
qingfx 发表于 2024-3-27 09:45
FHWX 发表于 2024-3-27 09:49
wjb6666 发表于 2024-3-27 09:53
感谢分享,收藏了
wangyp0506c 发表于 2024-3-27 09:55
感谢,待会运行试下
lopk666 发表于 2024-3-27 10:22
这个很有用的,感谢分享~
Tony2024 发表于 2024-3-27 10:30
感谢分享,收藏备用
bfbzfbz 发表于 2024-3-27 10:33
小白不会,有没有更加成熟的
fengmsn 发表于 2024-3-27 10:35
谢楼主分享
fire9 发表于 2024-3-27 10:40
感谢分享!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-27 20:20

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表