本帖最后由 小雨网络 于 2023-9-16 12:18 编辑
[Python] 纯文本查看 复制代码 import random
# 创建一个名言列表
quotes = [
"成功源于勇气,失败出于无能。",
"机会总是留给有准备的人。",
"坚持就是胜利。",
"生活就是不断前进的道路。",
"只要你努力,没有什么事情是不可能的。",
"相信自己,你就已经成功了一半。",
"智慧就是懂得选择。",
"人生短暂,珍惜每一刻。",
]
# 获取当前日期,用于每天选择一句名言
import datetime
today = datetime.date.today()
# 使用日期生成随机种子,以确保每天选择的名言不同
random.seed(today)
# 从名言列表中随机选择一句名言
selected_quote = random.choice(quotes)
# 打印选择的名言
print("每天一句至理名言:")
print(selected_quote)
2023年9月16日12:04:00 更新了一下代码 按照贴子里几个大神提供的信息改进了一下。提提意见
[Python] 纯文本查看 复制代码 import random
import datetime
import schedule
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import requests
# 发送邮件
def send_email(quote):
# 你的邮箱地址和密码
sender_email = "发信邮箱账号"
sender_password = "password"
# 收件人邮箱地址
recipient_email = "收件邮箱"
# 创建邮件内容
msg = MIMEMultipart()
msg["From"] = sender_email
msg["To"] = recipient_email
msg["Subject"] = "每天一句至理名言"
msg.attach(MIMEText(quote, "plain"))
# 连接SMTP服务器并发送邮件
try:
server = smtplib.SMTP("smtp.qq.com", 端口号)
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, recipient_email, msg.as_string())
server.quit()
print("名言邮件已发送成功!")
except Exception as e:
print("发送邮件时出错:", str(e))
# 获取随机名言
def get_random_quote():
try:
response = requests.get("接口地址")
if response.status_code == 200:
data = response.json()
return data.get("hitokoto", "没有名言数据")
else:
return "无法获取名言数据"
except Exception as e:
return "获取名言时出错:" + str(e)
# 定义定时任务
schedule.every().day.at("08:00").do(lambda: send_email(get_random_quote())) # 设置每天08:00发送邮件
# 循环执行定时任务
while True:
schedule.run_pending()
[Python] 纯文本查看 复制代码 import random
import datetime
import schedule
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import requests
# 发送邮件
def send_email(quote, sender_email, sender_password, recipient_email, smtp_server, smtp_port):
# 创建邮件内容
msg = MIMEMultipart()
msg["From"] = sender_email
msg["To"] = recipient_email
msg["Subject"] = "每天一句至理名言"
msg.attach(MIMEText(quote, "plain"))
try:
with smtplib.SMTP_SSL(smtp_server, smtp_port) as server:
server.login(sender_email, sender_password)
server.sendmail(sender_email, recipient_email, msg.as_string())
print("名言邮件已发送成功!")
except Exception as e:
print("发送邮件时出错:", str(e))
# 获取随机名言
def get_random_quote(api_url):
try:
response = requests.get(api_url)
if response.status_code == 200:
return response.text
else:
return "无法获取名言数据"
except Exception as e:
return "获取名言时出错:" + str(e)
# 定义定时任务
def schedule_email(sender_email, sender_password, recipient_email, smtp_server, smtp_port, api_url):
def job():
quote = get_random_quote(api_url)
send_email(quote, sender_email, sender_password, recipient_email, smtp_server, smtp_port)
schedule.every().day.at("08:00").do(job) # 设置每天08:00发送邮件
while True:
try:
schedule.run_pending()
# 检查是否到达指定的退出时间,例如下午5点
now = datetime.datetime.now().time()
if now.hour >= 17:
break
except KeyboardInterrupt:
break
if __name__ == '__main__':
# 请在下面填写您的邮箱和配置信息
sender_email = "----------" # 发信邮箱账号
sender_password = "----------" # 发信邮箱密码或应用程序密码
recipient_email = "----------" # 收信邮箱
smtp_server = "smtp.qq.com" # SMTP服务器地址,根据您的邮箱提供商进行更改
smtp_port = 465 # SMTP服务器端口号,根据您的邮箱提供商进行更改
api_url = "https://v1.hitokoto.cn/?c=f&encode=text" # 名言API接口地址
schedule_email(sender_email, sender_password, recipient_email, smtp_server, smtp_port, api_url)
改进后代码 |