Python 邮件发送一个带css样式的网页html
我之前有一篇关于Python发送邮件的博客,介绍了常规的发送邮件的方式
传送门
现在介绍一篇不同的方式,
背景,获取某个页面,然后将页面原封不动的通过邮件发送出去。用常规的方式,会被去除css样式后send
# -*- ecoding: utf-8 -*-
# @ModuleName: test005
# @Function:
# @Author: darling
# @Time: 2022-03-25 17:57
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from smtplib import SMTP_SSL
import requests
from loguru import logger
def sendmail(html_msg, receiver):
sender = 'sender@qq.com'
smtpserver = 'smtp.qq.com'
username = 'sender@qq.com'
password = 'qq邮箱授权码'
mail_title = '每日一闻'
message = MIMEText(html_msg, 'html', 'utf-8')
message['From'] = sender
message['To'] = receiver #这里是显示的收件箱名称,传str类型,多个用,隔开
message['Subject'] = Header(mail_title, 'utf-8')
try:
smtp = SMTP_SSL(smtpserver)
smtp.login(username, password)
# 这里的receiver 是实际的要发送出去的邮箱地址,单个用str 多个用list
smtp.sendmail(sender, receiver, message.as_string())
print("发送邮件成功!!!")
smtp.quit()
except smtplib.SMTPException:
print("发送邮件失败!!!")
def get_news(ip_address):
# 调用每日一文的接口
url = 'https://news.topurl.cn/?ip=%s' % ip_address
response = requests.get(url)
logger.info('===========返回数据:{}', response.text)
sendmail(response.text, 'receiver@qq.cn')
if __name__ == '__main__':
# 通过IP确定城市
get_news('112.64.11.28')
惜秦皇汉武,略输文采;唐宗宋祖,稍逊风骚。
一代天骄,成吉思汗,只识弯弓射大雕。
俱往矣,数风流人物,还看今朝