【水水水文章】用 Python 发邮件
水水水,文章,Python,发邮件
2025-03-13 21:27:20 时间
这是土土用 Python 发邮件的学习笔记
用 Python 推送每日天气
用到的 api : https://tianqiapi.com/
get_weather.py
import re
from datetime import datetime
from os import error
from urllib import request, error
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
try:
resp = request.urlopen("https://tianqiapi.com/api.php?style=tc&skin=pitaya")
html = resp.read().decode("utf-8")
weather = re.findall("<em>.+.</em>", html)[0]
print(weather)
print("okk")
except error.HTTPError as e:
print("error:{}".format(e.code))
sent_email.py
import smtplib
from email.mime.text import MIMEText
import get_weather
import time
#设置服务器所需信息
#邮箱服务器地址
mail_host = '`smtp.qiye.aliyun.com`'
#邮箱用户名
mail_user = '`tutu@hifurry.cn`'
#邮箱密码(部分邮箱为授权码)
mail_pass = 'your_email_password'
#邮件发送方邮箱地址
sender = '`tutu@hifurry.cn`'
#邮件接受方邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发
receivers = ['xxx@xxx.com']
#设置email信息
#邮件内容设置
message = MIMEText(get_weather.weather ,'html','utf-8')
#邮件主题
message['Subject'] = '{}-今日天气'.format(time.strftime("%Y/%m/%d", time.localtime()))
#发送方信息
message['From'] = sender
#接受方信息
message['To'] = receivers[0]
#登录并发送邮件
try:
smtpObj = smtplib.SMTP()
#连接到服务器
smtpObj.connect(mail_host,25)
#登录到服务器
smtpObj.login(mail_user,mail_pass)
#发送
smtpObj.sendmail(
sender,receivers,message.as_string())
#退出
smtpObj.quit()
print('success')
except smtplib.SMTPException as e:
print('error',e) #打印错误
用 Python 推兽图
青柠大佬在寒假写了一个每日推兽图的项目, 我突发奇想,通过py爬虫,自动将图发送到邮箱,
get_furry_img.py
import re
from datetime import datetime
from os import error
from urllib import request, error
import ssl
import time
ssl._create_default_https_context = ssl._create_unverified_context
try:
resp = request.urlopen("https://furry.lihouse.xyz")
html = resp.read().decode("utf-8")
pic_url = re.findall("<img class=\"full-bg\".+.jpeg\">", html)[0]
pic_url = "<style>img{{width:500px;}}</style>{}<p>查看图片详情:https://furry.lihouse.xyz/index.php?ftime={}".format(pic_url, time.strftime("%Y%m%d", time.localtime()))
print(pic_url)
print("okk")
except error.HTTPError as e:
print("error:{}".format(e.code))
sent_emall.py
import smtplib
from email.mime.text import MIMEText
import get_furry_img
import time
#设置服务器所需信息
mail_host = '`smtp.qiye.aliyun.com`'
mail_user = '`tutu@hifurry.cn`'
mail_pass = 'your_email_password'
sender = '`tutu@hifurry.cn`'
receivers = ['xxx@xxx.com']
#设置email信息
message = MIMEText(get_furry_img.pic_url ,'html','utf-8')
message['Subject'] = '{}-今日兽兽推送'.format(time.strftime("%Y/%m/%d", time.localtime()))
message['From'] = sender
message['To'] = receivers[0]
#登录并发送邮件
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host,25)
smtpObj.login(mail_user,mail_pass)
smtpObj.sendmail(
sender,receivers,message.as_string())
smtpObj.quit()
print('success')
except smtplib.SMTPException as e:
print('error',e)
。。。就是这样一篇水水的文章
相关文章
- 做自动化测试选择Python还是Java?
- python包合集-shutil
- python函数可以按照参数名称方式传递参数_python字符串作为函数参数
- 一对兔子从出生后第三个月起每个月_兔子繁衍问题python
- Python 打开文件对话框「建议收藏」
- python虚拟环境virtualenv_怎样用pycharm写代码
- pycharm安装opencv-python_pycharm opencv
- pycharm关闭自动补全_python opencv 教程
- 用python给女朋友表白_python绘制太阳花
- 工具推荐|利用python-cdo高效处理气象数据
- pycharm怎么看函数源代码_python查看第三方库的源码
- python不报错但计算不出结果_excel表格不能用公式怎么办
- node npm python 环境配置、安装
- python进入文件目录 命令_python创建目录
- pycharm远程调试python_pycharm调试教程
- USB 摄像头 进行python OpenCV 操作的基础设置【以yolo 目标检测为例】
- pycharm代码灰色_python import灰色
- python执行cmd命令并解析结果_python如何打包成可执行程序
- Python基础18-异常处理
- Python基础11-迭代器,生成器