[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()