前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【水水水文章】用 Python 发邮件

【水水水文章】用 Python 发邮件

作者头像
土土
发布2022-09-26 15:03:53
2540
发布2022-09-26 15:03:53
举报
文章被收录于专栏:geez

这是土土用 Python 发邮件的学习笔记

用 Python 推送每日天气

用到的 api : https://tianqiapi.com/

get_weather.py

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年2月26日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 用 Python 推送每日天气
  • 用 Python 推兽图
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档