Python程序教程

您现在的位置是:首页 >  Python

当前栏目

【水水水文章】用 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)

。。。就是这样一篇水水的文章


参考资料:https://zhuanlan.zhihu.com/p/24180606