最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python 發(fā)送郵件的四種方法匯總

 更新時(shí)間:2020年12月02日 11:46:44   作者:小公瑾  
這篇文章主要介紹了python 發(fā)送郵件的四種方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下

  這里針對(duì)smtplib做了一系列封裝,可以完成以下四種場景:

  • 發(fā)送純文本的郵件
  • 發(fā)送html頁面的郵件
  • 發(fā)送帶附件文件的郵件
  • 發(fā)送能展示圖片的郵件

  以上四種場景,已經(jīng)做好了二次封裝,經(jīng)測試OK,使用時(shí)直接傳入對(duì)應(yīng)參數(shù)即可,直接上代碼

import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart


class SendEMail(object):
  """封裝發(fā)送郵件類"""

  def __init__(self, host, port, msg_from, pwd):

    self.msg_from = msg_from
    self.password = pwd

    # 郵箱服務(wù)器地址和端口
    self.smtp_s = smtplib.SMTP_SSL(host=host, port=port)

    # 發(fā)送方郵箱賬號(hào)和授權(quán)碼
    self.smtp_s.login(user=msg_from, password=pwd)

  def send_text(self, to_user, content, subject, content_type='plain'):
    """
    發(fā)送文本郵件
    :param to_user: 對(duì)方郵箱
    :param content: 郵件正文
    :param subject: 郵件主題
    :param content_type: 內(nèi)容格式:'plain' or 'html'
    :return:
    """
    msg = MIMEText(content, _subtype=content_type, _charset="utf8")

    msg["From"] = self.msg_from
    msg["To"] = to_user
    msg["subject"] = subject

    self.smtp_s.send_message(msg, from_addr=self.msg_from, to_addrs=to_user)

  def send_file(self, to_user, content, subject, reports_path, filename, content_type='plain'):
    """
    發(fā)送帶文件的郵件
    :param to_user: 對(duì)方郵箱
    :param content: 郵件正文
    :param subject: 郵件主題
    :param reports_path: 文件路徑
    :param filename: 郵件中顯示的文件名稱
    :param content_type: 內(nèi)容格式
    """

    file_content = open(reports_path, "rb").read()

    msg = MIMEMultipart()

    text_msg = MIMEText(content, _subtype=content_type, _charset="utf8")
    msg.attach(text_msg)

    file_msg = MIMEApplication(file_content)
    file_msg.add_header('content-disposition', 'attachment', filename=filename)
    msg.attach(file_msg)

    msg["From"] = self.msg_from
    msg["To"] = to_user
    msg["subject"] = subject

    self.smtp_s.send_message(msg, from_addr=self.msg_from, to_addrs=to_user)

  def send_img(self, to_user, subject, content, filename, content_type='html'):
    '''
    發(fā)送帶圖片的郵件
    :param to_user: 對(duì)方郵箱
    :param subject: 郵件主題
    :param content: 郵件正文
    :param filename: 圖片路徑
    :param content_type: 內(nèi)容格式
    '''
    subject = subject
    msg = MIMEMultipart('related')
    # Html正文必須包含<img src="cid:imageid" alt="imageid" width="100%" height="100%>
    content = MIMEText(content, _subtype=content_type, _charset="utf8")
    msg.attach(content)
    msg['Subject'] = subject
    msg['From'] = self.msg_from
    msg['To'] = to_user

    with open(filename, "rb") as file:
      img_data = file.read()

    img = MIMEImage(img_data)
    img.add_header('Content-ID', 'imageid')
    msg.attach(img)

    self.smtp_s.sendmail(self.msg_from, to_user, msg.as_string())

以上就是python 發(fā)送郵件的四種方法匯總的詳細(xì)內(nèi)容,更多關(guān)于python 發(fā)送郵件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 解決Keras自帶數(shù)據(jù)集與預(yù)訓(xùn)練model下載太慢問題

    解決Keras自帶數(shù)據(jù)集與預(yù)訓(xùn)練model下載太慢問題

    這篇文章主要介紹了解決Keras自帶數(shù)據(jù)集與預(yù)訓(xùn)練model下載太慢問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • PyTorch中 tensor.detach() 和 tensor.data 的區(qū)別詳解

    PyTorch中 tensor.detach() 和 tensor.data 的區(qū)別詳解

    今天小編就為大家分享一篇PyTorch中 tensor.detach() 和 tensor.data 的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • python 如何上傳包到pypi

    python 如何上傳包到pypi

    這篇文章主要介紹了python 如何上傳包到pypi,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • Python統(tǒng)計(jì)中文詞頻的四種方法小結(jié)

    Python統(tǒng)計(jì)中文詞頻的四種方法小結(jié)

    統(tǒng)計(jì)中文詞頻是Python考試中常見的操作,本文我們總結(jié)了四種常見的中文詞頻統(tǒng)計(jì)方法,并列出代碼,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-08-08
  • FastApi如何快速構(gòu)建一個(gè)web項(xiàng)目的實(shí)現(xiàn)

    FastApi如何快速構(gòu)建一個(gè)web項(xiàng)目的實(shí)現(xiàn)

    本文主要介紹了FastApi如何快速構(gòu)建一個(gè)web項(xiàng)目的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Python循環(huán)語句中else的用法總結(jié)

    Python循環(huán)語句中else的用法總結(jié)

    這篇文章給大家整理了關(guān)于Python中循環(huán)語句中else的用法,包括常規(guī)的 if else 用法、if else 快捷用法、與 for 關(guān)鍵字一起用、與 while 關(guān)鍵字一起用以及與 try except 一起用的用法總結(jié),有需要的朋友們可以參考借鑒。
    2016-09-09
  • Python實(shí)現(xiàn)基本線性數(shù)據(jù)結(jié)構(gòu)

    Python實(shí)現(xiàn)基本線性數(shù)據(jù)結(jié)構(gòu)

    這篇文章主要實(shí)現(xiàn)四種數(shù)據(jù)結(jié)構(gòu),分別是數(shù)組、堆棧、隊(duì)列、鏈表。大家都知道可以用C語言實(shí)現(xiàn)這幾種數(shù)據(jù)結(jié)構(gòu),其實(shí)Python也可以實(shí)現(xiàn),下面跟著小編一起來學(xué)習(xí)。
    2016-08-08
  • Python抓取通過Ajax加載數(shù)據(jù)的示例

    Python抓取通過Ajax加載數(shù)據(jù)的示例

    在網(wǎng)頁上,有一些內(nèi)容是通過執(zhí)行Ajax請(qǐng)求動(dòng)態(tài)加載數(shù)據(jù)渲染出來的,本文主要介紹了使用Python抓取通過Ajax加載數(shù)據(jù),感興趣的可以了解一下
    2023-05-05
  • python 根據(jù)時(shí)間來生成唯一的字符串方法

    python 根據(jù)時(shí)間來生成唯一的字符串方法

    今天小編就為大家分享一篇python 根據(jù)時(shí)間來生成唯一的字符串方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • 如何利用Tensorflow2進(jìn)行貓狗分類識(shí)別

    如何利用Tensorflow2進(jìn)行貓狗分類識(shí)別

    圖像分類是根據(jù)圖像的語義信息將不同類別圖像區(qū)分開來,是計(jì)算機(jī)視覺中重要的基本問題,下面這篇文章主要給大家介紹了關(guān)于如何利用Tensorflow2進(jìn)行貓狗分類識(shí)別的相關(guān)資料,需要的朋友可以參考下
    2022-06-06

最新評(píng)論

珲春市| 车险| 陇南市| 泽库县| 农安县| 安化县| 旺苍县| 武乡县| 宜城市| 丰县| 延吉市| 临西县| 景泰县| 宁德市| 凉山| 三江| 莒南县| 景宁| 交口县| 登封市| 南涧| 舟山市| 龙井市| 沂南县| 唐河县| 阳春市| 威远县| 黄大仙区| 伊川县| 安阳市| 贵溪市| 黑山县| 新晃| 教育| 邵阳县| 澜沧| 平昌县| 修武县| 保德县| 德江县| 安化县|