python傳送帶附件的郵件

2021-08-14 06:51:47 字數 1767 閱讀 2238

# coding=utf8

import os, smtplib

from email.mime.multipart import mimemultipart

from email.mime.text import mimetext

from email.header import header

class sendmail():

reportpath = os.path.join(os.getcwd(), 'report')

def getreport(self):

dirs = os.listdir(self.reportpath)

dirs.sort(key=lambda fn:os.path.getmtime(self.reportpath+'//'+fn))

report = os.path.join(self.reportpath,dirs[-1])

print report

return report

def sendmail(self,report):

# 定義伺服器型別,登入名密碼,傳送人和接受人,傳送郵件主題

smtpserver = 'smtp.qq.com'

user = '[email protected]'

passwd = 'odvfqndlamxobfhh'

sender = '[email protected]'

receiver = ['[email protected]','[email protected]','[email protected]']

content = '這是測試用的哦'

#構造附件內容

f = open(report,'rb')

sendfile = f.read()

f.close()

att = mimetext(sendfile,'base64','utf-8')

#外面用單引號,裡面檔名用雙引號

att["content-disposition"] = 'attachment;filename="1.txt"'

msgroot = mimemultipart()

msgroot.attach(mimetext(content,'html','utf-8'))

msgroot['from'] = sender

msgroot['to'] = ','.join(receiver)

msgroot['subject'] = header(header,'utf-8')

msgroot.attach(att)

# 傳送郵件,埠用465, keyfile = 'vxkdfejinpifbeaj'

smtp = smtplib.smtp_ssl(smtpserver, 465)

smtp.helo(smtpserver) # 向伺服器標識使用者身份

smtp.ehlo(smtpserver) # 伺服器返回結果確認

# 直接登入,之前是登入前寫了smtp.connect()

smtp.login(user, passwd)

smtp.sendmail(sender, receiver, msgroot.as_string())

smtp.quit()

if __name__ == "__main__":

mail = sendmail()

report = mail.getreport()

mail.sendmail(report)

python傳送帶附件郵件

1.不包括附件的郵件 coding utf 8 import smtplib import string 傳送普通的文字郵件 郵件smtp的位址 host smtp.163.com 定義郵件的標題 subject 這是郵件標題 發件人 from 163.com 收件人 to qq.com 傳送的郵件...

Python 傳送帶附件的郵件

usr bin python3 import smtplib from email.mime.text import mimetext from email.mime.multipart import mimemultipart from email.header import header sen...

python 傳送郵件帶附件 封裝

usr bin python coding utf 8 import smtplib smtplib這個模組是管發郵件 from email.mime.text import mimetext 構造郵件內容 from email.mime.multipart import mimemultipart...