Receive email
Copy codeThe Code is as follows:
Import poplib, pdb, email, re, time
From email import header
POP_ADDR = r 'pop .126.com'
USER =''
PASS =''
CONFIG =''
Def getYear (date ):
Rslt = re. search (R' \ b2 \ d {3} \ B ', date)
Return int (rslt. group ())
Def getMonth (date ):
MonthMap = {'Jan ': 1, 'feb': 2, 'mar': 3, 'apr': 4, 'may': 5, 'jun': 6,
'Jul': 7, 'aug': 8, 'sept': 9, 'oct': 10, 'nov': 11, 'dec ': 12 ,}
Rslt = re. findall (R' \ B \ w {3} \ B ', date)
For I in range (len (rslt )):
Month = monthMap. get (rslt [I])
If None! = Month:
Break
Return month
Def getDay (date ):
Rslt = re. search (R' \ B \ d {1, 2} \ B ', date)
Return int (rslt. group ())
Def getTime (date ):
Rslt = re. search (R' \ B \ d {2 }:\ d {2 }:\ d {2} \ B ', date)
TimeList = rslt. group (). split (':')
For I in range (len (timeList )):
TimeList [I] = int (timeList [I])
Return timeList
Def transformDate (date ):
Rslt = getYear (date)
Rslt = rslt * 100
Rslt = rslt + getMonth (date)
Rslt = rslt * 100
Rslt = rslt + getDay (date)
TimeList = getTime (date)
For I in range (len (timeList )):
Rslt = rslt * 100
Rslt = rslt + timeList [I]
Print (rslt)
Return rslt
Def getRecentReadMailTime ():
Fp = open (CONFIG, 'R ')
RrTime = fp. read ()
Fp. close ()
Return rrTime
Def setRecentReadMailTime ():
Fp = open (CONFIG, 'w ')
Fp. write (time. ctime ())
Fp. close ()
Return
Def parseMailSubject (msg ):
SubSrt = msg. get ('subobject ')
If None = subSrt:
Subject = 'no theme'
Else:
SubList = header. decode_header (subSrt)
Subinfo = subList [0] [0]
Subcode = subList [0] [1]
If isinstance (subinfo, bytes ):
Subject = subinfo. decode (subcode)
Else:
Subject = subinfo
Print (subject)
Def parseMailContent (msg ):
If msg. is_multipart ():
For part in msg. get_payload ():
ParseMailContent (part)
Else:
BMsgStr = msg. get_payload (decode = True)
Charset = msg. get_param ('charset ')
MsgStr = 'Code failed'
Try:
If None = charset:
MsgStr = bMsgStr. decode ()
Else:
MsgStr = bMsgStr. decode (charset)
Except t:
Pass
Print (msgStr)
Def recvEmail ():
Server = poplib. POP3 (POP_ADDR)
Server. user (USER)
Server. pass _ (PASS)
MailCount, size = server. stat ()
MailNoList = list (range (mailCount ))
MailNoList. reverse ()
HisTime = transformDate (getRecentReadMailTime ())
SetRecentReadMailTime ()
# Pdb. set_trace ()
For I in mailNoList:
Message = server. retr (I + 1) [1]
Mail = email. message_from_bytes (B '\ n'. join (message ))
If transformDate (mail. get ('date')> hisTime:
ParseMailSubject (mail)
# ParseMailContent (mail)
Else:
Break
RecvEmail ()
Send Email:
Copy codeThe Code is as follows:
Import OS, pdb, smtplib, time, mimetypes
From email. header import Header
From email. mime. multipart import MIMEMultipart
From email. mime. text import MIMEText
From email. mime. audio import MIMEAudio
From email. mime. image import MIMEImage
COMMASPACE = ','
SONG_PATH = r''
RECORD_FILE =''
PIC_PATH =''
CC = []
TO = []
ME =''
SMTP_SERVER = 'smtp .126.com'
USER =''
PASS =''
Def constructAddr (addrList ):
Return COMMASPACE. join (addrList)
Def willChooseThisMedia (media, path ):
Fp = open (path + RECORD_FILE, 'R ')
Raise info = fp. read ()
Fp. close ()
Using infolist = using info. split ('\ n ')
If media not in your infolist:
Fp = open (path + RECORD_FILE, 'A ')
Fp. write (media + '\ n ')
Fp. close ()
Return True
Else:
Return False
Def getTodayMedia (path ):
MediaList = OS. listdir (path)
For media in mediaList:
If False = OS. path. isfile (path + media ):
Continue
Else:
If (media. endswith ('mp3') or media. lower (). endswith ('jpg ') and \
WillChooseThisMedia (media, path ):
Return media
Def getMIMEImage (pic ):
Fp = open (PIC_PATH + pic, 'rb ')
ImageType = mimetypes. guess_type (PIC_PATH + pic)
Image = MIMEImage (fp. read (), imageType [0]. split ('/') [1])
Fp. close ()
Image. add_header ('content-disposition', 'attachment ')
Image. set_param ('filename', pic, header = 'content-disposition', charset = 'gb2312 ')
Return image
Def getMIMEAudio (song ):
Fp = open (SONG_PATH + song, 'rb ')
AudioType = mimetypes. guess_type (SONG_PATH + song)
Audio = MIMEAudio (fp. read (), audioType [0]. split ('/') [1])
Fp. close ()
Audio. add_header ('content-disposition', 'attachment ')
Audio. set_param ('filename', song, header = 'content-disposition', charset = 'gb2312 ')
Return audio
Def constructMail ():
Mail = MIMEMultipart ()
Song = getTodayMedia (SONG_PATH)
Pic = getTodayMedia (PIC_PATH)
MailSubject = Header ('share today | '+ song, 'utf-8 ')
MailDate = Header (time. ctime ())
Mail ['subobject'] = mailSubject
Mail ['date'] = mailDate
Mail ['to'] = constructAddr ()
Mail ['cc'] = constructAddr (cc)
Mail ['from'] = ME
MailBody = MIMEText (song, _ charset = 'gb2312 ')
Mail. attach (mailBody)
Mail. attach (getMIMEAudio (song ))
Mail. attach (getMIMEImage (pic ))
Return mail
Def sendMail ():
Session = smtplib. SMTP (SMTP_SERVER)
Session. login (USER, PASS)
Mail = constructMail ()
Session. sendmail (ME, constructAddr (TO), mail. as_string ())
Session. quit ()
SendMail ()