python 爱心邮件代码

devtools/2024/11/17 18:20:59/
python">import smtplib
import time
from email.mime.text import MIMEText
import requests
from lxml import etree
import datetime
from requests.exceptions import RequestException# 邮件配置
sender_maile = ''  # 发件人地址
sender_pass = ''  # 邮件授权码
boy_name = ''  # 发件人姓名
girl_name = ''  # 收件人姓名
receiver_mail = ''  # 收件人邮箱
special_day = '2023-10-11'  # 纪念日
province = 'guangxi'  # 省份
city = 'jiangnan-district'  # 城市
title = ''  # 邮件主题# 请求头
header = {'Referer': 'https://tianqi.moji.com/weather/china/guangxi','User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
}
session = requests.session()# 获取纪念日距今天数
def get_day():d1 = datetime.datetime.strptime(special_day, '%Y-%m-%d')d2 = datetime.datetime.strptime(datetime.datetime.now().strftime('%Y-%m-%d'), '%Y-%m-%d')delta = d2 - d1return delta.days# 获取土味情话
def get_chp():url = "https://api.lovelive.tools/api/SweetNothings"try:resp = requests.get(url=url, verify=True)return resp.textexcept RequestException:return "获取土味情话失败"# 获取天气提醒
def get_weathertip():try:url = f'https://tianqi.moji.com/weather/china/{province}/{city}'resp = session.get(url=url, headers=header, verify=False)html = etree.HTML(resp.text)em = html.xpath('/html/body/div[4]/div[1]/div[4]/em/text()')[0]return emexcept:return "获取天气提醒失败"# 获取天气信息
def get_weather():try:url = f'https://tianqi.moji.com/weather/china/{province}/{city}'resp = session.get(url=url, headers=header, verify=False)html = ''htmls = etree.HTML(resp.text)ul = htmls.xpath('/html/body/div[5]/div[1]/div[1]/ul')for lis in ul:day = lis.xpath('./li[1]/a/text()')[0]src = lis.xpath('./li[2]/span/img/@src')[0]temperature = lis.xpath('./li[3]/text()')[0]air = lis.xpath('./li[5]/strong/text()')[0].strip()color = str(lis.xpath('./li[5]/strong/@class')[0])color = {'level_1': '#8fc31f','level_2': '#d7af0e','level_3': '#f39800','level_4': '#e2361a','level_5': '#5f52a0','level_6': '#631541'}.get(color, '#000000')html += f"""<div style="display: flex;margin-top:5px;height: 30px;line-height: 30px;justify-content: space-around;align-items: center;"><span style="width:15%; text-align:center;">{day}</span><div style="width:10%; text-align:center;"><img style="height:26px;vertical-align:middle;" src='{src}' alt=""></div><span style="width:25%; text-align:center;">{temperature}</span><div style="width:35%; "><span style="display:inline-block;padding:0 8px;line-height:25px;color:{color}; border-radius:15px; text-align:center;">{air}</span></div></div>"""return htmlexcept:return "获取天气失败"# 获取每日图片
def get_image():url = "http://wufazhuce.com/"try:resp = requests.get(url=url)html = etree.HTML(resp.text)img_url = html.xpath('//*[@id="carousel-one"]/div/div[1]/a/img/@src')[0]return img_urlexcept:return "https://example.com/default-image.jpg"  # 替换为默认图片链接# 获取当天日期
def get_today():i = datetime.datetime.now()date = f"{i.year}/{i.month}/{i.day}"return date# 邮件内容
mail_content = f"""<!DOCTYPE html>
<html>
<head><meta charset="UTF-8">
</head>
<body style="margin:0;padding:0;"><div style="width:100%; margin: 40px auto;font-size:20px; color:#5f5e5e;text-align:center"><span>今天是我们在一起的第</span><span style="font-size:24px;color:rgb(221, 73, 73)">{get_day()}</span><span>天</span></div><div style="width:100%; margin: 0 auto;color:#5f5e5e;text-align:center"><span style="display:block;color:#676767;font-size:20px">{get_chp()}</span><br><span style="display:block;color:#676767;font-size:20px">{get_weathertip()}</span><span style="display:block;margin-top:15px;color:#676767;font-size:15px">近期天气预报</span>{get_weather()}</div><div style="text-align:center;margin:35px 0;"><span style="display:block;margin-top:55px;color:#676767;font-size:15px">{boy_name} ❤️ {girl_name}</span><span style="display:block;margin-top:25px;font-size:22px; color:#9d9d9d;">{get_today()}</span><img src='{get_image()}' style="width:100%;margin-top:10px;" alt=""></div>
</body>
</html>"""# 发送邮件
def send_mail():try:maile_obj = smtplib.SMTP_SSL('smtp.qq.com', 465)maile_obj.login(sender_maile, sender_pass)msg = MIMEText(mail_content, _subtype='html', _charset='utf-8')msg['Subject'] = titlemsg['From'] = f"{boy_name} <{sender_maile}>"msg['To'] = f"{girl_name} <{receiver_mail}>"maile_obj.sendmail(sender_maile, receiver_mail, msg.as_string())maile_obj.quit()print("发送成功!")except smtplib.SMTPException as e:print("邮件发送失败:", e)if __name__ == '__main__':send_mail()print("当前时间:", datetime.datetime.now())

请添加图片描述


http://www.ppmy.cn/devtools/134757.html

相关文章

rust逆向初探

rust 逆向葵花宝典 rust逆向技巧 rust逆向三板斧&#xff1a; [!NOTE] 快速定位关键函数 (真正的main函数)&#xff1a;观察输出、输入&#xff0c;字符串搜索&#xff0c;断点等方法。定位关键 加密区 &#xff1a;根据输入的flag&#xff0c;打硬件断点&#xff0c;快速捕获…

GRE做题笔记(零散的个人经验)

locomotive机车By 1813, the Luddite resistance had all but vanished. all but表示“几乎完全”的程度&#xff0c;或者表示排除piston活塞attributed to 归因于how a sportsperson accounted for their own experience of stress 运动员如何解释自己的压力经历 &#xff0c;…

防火墙----iptables

防火墙是位于内部网和外部网之间的屏障&#xff0c;他按照系统管理员预先定义好的规则来控制数据包的进出 一、iptables简介 防火墙会从以上至下的顺序来读取配置的策略规则&#xff0c;在找到匹配项后就立即结束匹配工作并去执行匹配项中定义的行为&#xff08;即放行或阻止&…

POI和easyExcel的讲解和使用

目录 应用场景&#xff1a; Apache POI&#xff1a; EasyExcel&#xff1a; POI具体实现&#xff1a; POI-Excel 写&#xff1a; POI-Excel 读&#xff1a; 大文件写HSSF&#xff1a; 大文件写XSSF&#xff1a; 大文件写SXSSF&#xff1a; EasyExcel&#xff1a; 导…

Python练习19

Python日常练习 题目&#xff1a; 打印如下九九乘法表 1*11 2*12 2*24 3*13 3*26 3*39 4*14 4*28 4*312 4*416 5*15 5*210 5*315 5*420 5*525 6*16 6*212 6*318 6*424 6*530 6*636 7*17 7*214 7*321 7*428 7*535 7*642 7*749 8*18 8*216 8*324 8*432 8*540 8*648 8*756 8*86…

c# 调用c++ 的dll 出现找不到函数入口点

今天在调用一个设备的dll文件时遇到了一点波折&#xff0c;因为多c 不熟悉&#xff0c;调用过程张出现了找不到函数入口点&#xff0c;一般我们使用c# 调用c 文件&#xff0c;还是比较简单。 [DllImport("AtnDll2.dll",CharSet CharSet.Ansi)]public static extern …

k8s更新

k8s更新 1.30 升级了 Metrics Server 到 v0.7.0 kubectl get cronjob命令增加了时区列显示 kubectl describe命令在描述VolumeAttributesClass、作业、Pod 模板等时提供了更多信息&#xff0c;有助于深入排查问题。 改进了kubectl logs命令&#xff0c;当 Pod 未找到时会显示所…

【HarmonyOS NEXT】实战——登录页面

【HarmonyOS NEXT】实战——登录页面 在本文中&#xff0c;我们将深入探讨如何使用HarmonyOS NEXT来实现一个功能完备的登录页面。通过这个实战案例&#xff0c;你将结合页面布局、数据本地化存储、网络请求等多方面了解到HarmonyOS NEXT在构建现代应用时的强大能力和灵活性。…