Traditional Email contains two different parts: Header & Body. The Header contains the control data. The Body contains only the information of the letter. The starting part of an email is always the Header, and the subsequent part is the Body.
Whether using Python to parse emails or send emails, it is essential to understand the Header. The following is a brief introduction:
From Header: indicates the sender of the email to the user.
Reply-To Header: Set a Reply address.
Subject Header: used to display the mailbox Abstract
Date Header: used to record the time
Message-ID Header: Helps certain programs implement clues.
MIME Header: select the appropriate language and format
The following code indicates that the supervisor writes an email and parses the email that was just written by the supervisor:
[Python]
# This is the test for email
Import sys, traceback, email
From email. MIMEText import MIMEText
From email import Utils
Def sendEmail ():
Try:
Print ('input the message what U want to say :')
Mess = sys. stdin. readline (). rstrip ()
Msg = MIMEText (mess)
Msg ['to'] = '2017 @ qq.com'
Msg ['from'] = '2017 @ qq.com'
Msg ['date'] = Utils. formatdate (localtime = 1)
Msg ['message-id'] = Utils. make_msgid ()
Print msg. as_string ()
Log = open ('log', 'w ')
Log. write (msg. as_string ())
Except t:
Traceback. print_exc ()
Sys. exit (1)
Def recvEmail ():
Try:
Msg = email. message_from_file ('log ')
Print ('headers from emails :')
For header, value in msg. items ():
Print header + ':' + value
If msg. is_multipart ():
Print ('this program cannot handle this ')
Sys. exit (1)
Print (msg. get_payload ())
Except t:
Traceback. print_exc ()
Sys. exit (1)
P.s. it seems that from the beginning of the topic, the code to be tested is very simple, and it may not be nutritious for those who often come into contact with Python. But for new users like me, I should first learn Python, this learning method may be suitable for you to understand and complete a complex function (compared with your previous code and capabilities) application. In a word, we should step by step.
From the column of FishinLab