欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python3實現(xiàn)帶多張圖片、附件的郵件發(fā)送

 更新時間:2019年08月10日 16:19:26   作者:SoaringXu  
這篇文章主要為大家詳細介紹了python3實現(xiàn)帶多張圖片、附件的郵件發(fā)送,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了python3實現(xiàn)多張圖片附件郵件發(fā)送的具體代碼,供大家參考,具體內(nèi)容如下

直接上代碼,沒有注釋!

from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header

class Mail(object):
  def __init__(self, host, nickname, username, password, postfix):
    self.host = host
    self.nickname = nickname
    self.username = username
    self.password = password
    self.postfix = postfix

  def send_mail(self, to_list, subject, content, cc_list=[], encode='gbk', is_html=True, images=[]):
    me = str(Header(self.nickname, encode)) + "<" + self.username + "@" + self.postfix + ">"
    msg = MIMEMultipart()
    msg['Subject'] = Header(subject, encode)
    msg['From'] = me
    msg['To'] = ','.join(to_list)
    msg['Cc'] = ','.join(cc_list)
    if is_html:
      mail_msg = ''
      for i in range(len(images)):
        mail_msg += '<p><img src="cid:image%d" height="240" width="320"></p>' % (i+1)
      msg.attach(MIMEText(content + mail_msg, 'html', 'utf-8'))

      for i, img_name in enumerate(images):
        with open(img_name, 'rb') as fp:
          img_data = fp.read()
        msg_image = MIMEImage(img_data)
        msg_image.add_header('Content-ID', '<image%d>' % (i+1))
        msg.attach(msg_image)
        # 將圖片作為附件
        # image = MIMEImage(img_data, _subtype='octet-stream')
        # image.add_header('Content-Disposition', 'attachment', filename=images[i])
        # msg.attach(image)
    else:
      msg_content = MIMEText(content, 'plain', encode)
      msg.attach(msg_content)

    try:
      s = smtplib.SMTP()
      # s.set_debuglevel(1)
      s.connect(self.host)
      s.login(self.username, self.password)
      s.sendmail(me, to_list + cc_list, msg.as_string())
      s.quit()
      s.close()
      return True
    except Exception as e:
      print(e)
      return False

def send_mail(to_list, title, content, cc_list=[], encode='utf-8', is_html=True, images=[]):
  content = '<pre>%s</pre>' % content
  m = Mail('smtp.163.com', 'TV-APP TEST', 'tvapp_qa', 'ujlnluutpfespgxz', '163.com')
  m.send_mail(to_list, title, content, cc_list, encode, is_html, images)


if __name__ == '__main__':
  images = [
    '1.png',
    '2.png',
    '3.png',
    '4.png',
  ]
  import time
  title = 'new images %s' % time.strftime('%H:%M:%S')
  content = 'this is attach images %s' % time.time()
  send_mail(['x@163.com'], title, content, ['xx@163.com', 'xxx@163.com'], 'utf-8', True, images)

后記

調(diào)試發(fā)送多張圖片的時候遇到的問題:

用for循環(huán)生成的mail_msg,不能直接attach,需要和content一起attach

mail_msg = ''
for i in range(len(images)):
  mail_msg += '<p><img src="cid:image%d" height="240" width="320"></p>' % (i+1)
  msg.attach(MIMEText(**content** + mail_msg, 'html', 'utf-8'))

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Django中常用的查詢數(shù)據(jù)方法及查詢對象的條件詳解

    Django中常用的查詢數(shù)據(jù)方法及查詢對象的條件詳解

    在web 開發(fā)過程中,Django 與后臺數(shù)據(jù)庫的交互是必不可少的一項,也是實現(xiàn)業(yè)務邏輯所需數(shù)據(jù)的重要方式,這篇文章主要給大家介紹了關于Django中常用的查詢數(shù)據(jù)方法及查詢對象條件的相關資料,需要的朋友可以參考下
    2021-09-09
  • Python編程實現(xiàn)超炫動態(tài)排序圖

    Python編程實現(xiàn)超炫動態(tài)排序圖

    這篇文章主要介紹了Python編程實現(xiàn)超炫動態(tài)排序圖的示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2021-10-10
  • Python實現(xiàn)計算長方形面積(帶參數(shù)函數(shù)demo)

    Python實現(xiàn)計算長方形面積(帶參數(shù)函數(shù)demo)

    今天小編就為大家分享一篇Python實現(xiàn)計算長方形面積(帶參數(shù)函數(shù)demo),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python3 執(zhí)行系統(tǒng)命令并獲取實時回顯功能

    Python3 執(zhí)行系統(tǒng)命令并獲取實時回顯功能

    這篇文章主要介紹了Python3 執(zhí)行系統(tǒng)命令并獲取實時回顯功能,文中通過兩種方法給大家介紹了Python執(zhí)行系統(tǒng)命令并獲得輸出的方法,需要的朋友可以參考下
    2019-07-07
  • Python3的socket使用方法詳解

    Python3的socket使用方法詳解

    這篇文章主要介紹了Python3的socket使用方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • 對python使用http、https代理的實例講解

    對python使用http、https代理的實例講解

    今天小編就為大家分享一篇對python使用http、https代理的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • 使用opencv中匹配點對的坐標提取方式

    使用opencv中匹配點對的坐標提取方式

    這篇文章主要介紹了使用opencv中匹配點對的坐標提取方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • django執(zhí)行原始查詢sql,并返回Dict字典例子

    django執(zhí)行原始查詢sql,并返回Dict字典例子

    這篇文章主要介紹了django執(zhí)行原始查詢sql,并返回Dict字典例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Flask自定義序列化超詳細講解

    Flask自定義序列化超詳細講解

    序列化其實就是將數(shù)據(jù)轉(zhuǎn)化成一種可逆的數(shù)據(jù)結構,自然,逆向的過程就叫做反序列化。php將數(shù)據(jù)序列化和反序列化會用到兩個函數(shù):serialize 將對象格式化成有序的字符串、unserialize 將字符串還原成原來的對象
    2022-11-11
  • Python數(shù)據(jù)類型詳解(三)元祖:tuple

    Python數(shù)據(jù)類型詳解(三)元祖:tuple

    本文給大家介紹的是Python數(shù)據(jù)類型中的元祖(tuple),簡單的說Tuple,與列表一樣,元素也是不可變的,但與列表不同,在一個元祖可以包含不同類型的元素
    2016-05-05

最新評論