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')

posted @ 2022-04-04 16:26  darling331  阅读(528)  评论(0编辑  收藏  举报