以下是一个示例代码,用于根据日期发送电子邮件提醒:
import datetime
import smtplib
from email.mime.text import MIMEText
def send_email(subject, message, from_email, to_email):
msg = MIMEText(message)
msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = to_email
# 输入你的邮箱地址和密码
username = 'your_email@example.com'
password = 'your_password'
# 使用SMTP服务器发送邮件
server = smtplib.SMTP('smtp.example.com', 587)
server.starttls()
server.login(username, password)
server.sendmail(from_email, to_email, msg.as_string())
server.quit()
# 获取当前日期
current_date = datetime.date.today()
# 为需要发送提醒的日期创建一个字典
reminder_dates = {
'2022-01-01': 'Happy New Year!',
'2022-12-25': 'Merry Christmas!'
}
# 检查当前日期是否在提醒日期字典中
if str(current_date) in reminder_dates:
subject = 'Reminder'
message = reminder_dates[str(current_date)]
from_email = 'your_email@example.com'
to_email = 'recipient@example.com'
send_email(subject, message, from_email, to_email)
请注意,你需要替换代码中的以下部分:
your_email@example.com
:你的邮箱地址your_password
:你的邮箱密码smtp.example.com
:你的SMTP服务器地址recipient@example.com
:接收提醒邮件的收件人的邮箱地址此外,你还可以根据需要修改提醒日期字典reminder_dates
,以适应你的实际需求。