Python send mail receive sample share _python

Source: Internet
Author: User

Receive mail

Copy Code code 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 = {' No ': 1, ' Feb ': 2, ' Mar ': 3, ' APR ': 4, ' May ': 5, ' June ': 6,
' June ': 7, ' Aug ': 8, ' SEP ': 9, ' Oct ': Ten, ' Nov ': One, ' 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 (' subject ')
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 = ' Decode Failed '
Try
If None = CharSet:
Msgstr = Bmsgstr.decode ()
Else
Msgstr = Bmsgstr.decode (CharSet)
Except
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 mail:

Copy Code code 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 ')
Shareinfo = Fp.read ()
Fp.close ()

Shareinfolist = Shareinfo.split (' \ n ')

If media not in Shareinfolist:
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[' subject '] = Mailsubject
mail[' Date ' = Maildate
Mail[' to '] = constructaddr (To)
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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.