Delphi uses python to decode emails. Delphi uses python to decode emails.

Source: Internet
Author: User

 

Delphi uses python to decode emails (reproduced)

It is estimated that many people, like me, will not be able to handle many bugs when using Indy, and have to use and modify it.

Recently, Indy has been used to send and receive emails, from D7 to d2007 to d2010. Later, an latest version of SVN was detected. It is surprising that some bugs have been fixed in the new version, it is frustrating that some new bugs have been introduced in the new version.

A few days ago, I simply read Python and tested it. I found that python's email processing is very concise! And receiving and decoding are much better than Indy, so I want to use python to process emails. But because the project uses Delphi, I searched the internet and found pythonfordelphi. I used it for a while. It was good, so I used python for decoding, then it is called in Delphi.

First, we will introduce related resources:

Python4delphi:

Http://mmm-experts.com/

Entry:

Http://www.atug.com/andypatterns/pythonDelphiTalk.htm

 

Oh, it has settled on Google and Yahoo:

Http://tech.groups.yahoo.com/group/pythonfordelphi/

Http://code.google.com/p/python4delphi/

 

Let's take a look at the Python code:

 

1 import email
2
3 fp = open ("test. eml", "R") # open an EML file
4 MSG = Email. message_from_file (FP)
5 # MSG = Email. message_from_string (STR) # You can also create a string
6 subject = MSG. Get ("subject") # The subject in the receiving object header, that is, the topic.
7 # The following three lines of code are only used to decode the image =? GBK? Q? = Cf = e0 = C6 = ac? = Subject
8 h = Email. header. header (subject)
9 DH = Email. header. decode_header (h)
10 subject = DH [0] [0]
11 print "Subject:", Subject
12 print "from:", email. utils. parseaddr (msg. Get ("from") [1] # Get from
13 print "to:", email. utils. parseaddr (msg. Get ("to") [1] # Get
14
15 # data blocks of each mime in a loop letter
16 I = 0
17 textplain =''
18 texthtml =''
19
20 For par in MSG. Walk ():
21 if not par. is_multipart (): # If you want to determine whether it is a multipart, the data in it is useless. You can understand mime-related knowledge.
22 name = par. get_param ("name") # if it is an attachment, the file name of the attachment will be retrieved here
23 Print Name
24
25 if name:
26 print 'attachments '+ name # Here, no attachments are processed. Only text content is processed.
27 else:
28 # text content instead of Attachments
29 # print par. get_payload (decode = true) # decode the text and output it directly.
30 content_type = par. get_content_type ()
31 if content_type in ['text/plain ']:
32 textplain = par. get_payload (decode = true)
33 textplain. value = textplain # textplain. Value and the following texthtml. value cannot be correctly executed in normal Python and are objects in pytho4delphi.
34 if content_type in ['text/html ']:
35 texthtml = par. get_payload (decode = true)
36 texthtml. value = texthtml
37
38 FP. Close ()
39

 

 

Okay. The above is just an example of handling the text content.

 

In Delphi, two tpythondelphivar values are added, textplain and texthtm, respectively. Then:

 

1 PE. execstrings (memo1.lines); // memo1 indicates the above Python code, which can be put in the file.
2 showmessage (texthtml. valueasstring); // at this time, texthtml. valueasstring is the hypertext content of the decoded email. If there is text/plain content, take the value of textplain.

 

 

Okay, don't get angry with Indy any more!

It is estimated that many people, like me, will not be able to handle many bugs when using Indy, and have to use and modify it.

Recently, Indy has been used to send and receive emails, from D7 to d2007 to d2010. Later, an latest version of SVN was detected. It is surprising that some bugs have been fixed in the new version, it is frustrating that some new bugs have been introduced in the new version.

A few days ago, I simply read Python and tested it. I found that python's email processing is very concise! And receiving and decoding are much better than Indy, so I want to use python to process emails. But because the project uses Delphi, I searched the internet and found pythonfordelphi. I used it for a while. It was good, so I used python for decoding, then it is called in Delphi.

First, we will introduce related resources:

Python4delphi:

Http://mmm-experts.com/

Entry:

Http://www.atug.com/andypatterns/pythonDelphiTalk.htm

 

Oh, it has settled on Google and Yahoo:

Http://tech.groups.yahoo.com/group/pythonfordelphi/

Http://code.google.com/p/python4delphi/

 

Let's take a look at the Python code:

 

1 import email
2
3 fp = open ("test. eml", "R") # open an EML file
4 MSG = Email. message_from_file (FP)
5 # MSG = Email. message_from_string (STR) # You can also create a string
6 subject = MSG. Get ("subject") # The subject in the receiving object header, that is, the topic.
7 # The following three lines of code are only used to decode the image =? GBK? Q? = Cf = e0 = C6 = ac? = Subject
8 h = Email. header. header (subject)
9 DH = Email. header. decode_header (h)
10 subject = DH [0] [0]
11 print "Subject:", Subject
12 print "from:", email. utils. parseaddr (msg. Get ("from") [1] # Get from
13 print "to:", email. utils. parseaddr (msg. Get ("to") [1] # Get
14
15 # data blocks of each mime in a loop letter
16 I = 0
17 textplain =''
18 texthtml =''
19
20 For par in MSG. Walk ():
21 if not par. is_multipart (): # If you want to determine whether it is a multipart, the data in it is useless. You can understand mime-related knowledge.
22 name = par. get_param ("name") # if it is an attachment, the file name of the attachment will be retrieved here
23 Print Name
24
25 if name:
26 print 'attachments '+ name # Here, no attachments are processed. Only text content is processed.
27 else:
28 # text content instead of Attachments
29 # print par. get_payload (decode = true) # decode the text and output it directly.
30 content_type = par. get_content_type ()
31 if content_type in ['text/plain ']:
32 textplain = par. get_payload (decode = true)
33 textplain. value = textplain # textplain. Value and the following texthtml. value cannot be correctly executed in normal Python and are objects in pytho4delphi.
34 if content_type in ['text/html ']:
35 texthtml = par. get_payload (decode = true)
36 texthtml. value = texthtml
37
38 FP. Close ()
39

 

 

Okay. The above is just an example of handling the text content.

 

In Delphi, two tpythondelphivar values are added, textplain and texthtm, respectively. Then:

 

1 PE. execstrings (memo1.lines); // memo1 indicates the above Python code, which can be put in the file.
2 showmessage (texthtml. valueasstring); // at this time, texthtml. valueasstring is the hypertext content of the decoded email. If there is text/plain content, take the value of textplain.

 

 

Okay, don't get angry with Indy any more!

Related Article

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.