Python network programming test-Email discussion

Source: Internet
Author: User

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

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.