Python论坛  - 讨论区

标题:RE: [python-chinese] 请教读取email的办法

2006年07月04日 星期二 11:24

liguopeng liguopeng at tsinghua.org.cn
Tue Jul 4 11:24:00 HKT 2006

我对这个比较有兴趣。不过python的经验比较少,呵呵。

C++不错,MIME, POP3, SMTP等也比较熟。

 

  _____  

From: python-chinese-bounces at lists.python.cn
[mailto:python-chinese-bounces at lists.python.cn] On Behalf Of netkiller
Sent: 2006年7月4日 10:48
To: python-chinese at lists.python.cn
Subject: Re: [python-chinese] 请教读取email的办法

 

正责.给你参考.我在写一个基于数据库的新闻组..你有兴趣吗?

 

你的需求我很早以前做过..因为硬盘坏了,例子没了..你可以去网上找找我的贴
子.

 

    def parseArticle(self,data):
        """
        From: "NEO" <openunix at 163.com>
        Newsgroups: cn.test
        Subject: test
        Date: Fri, 30 Jun 2006 17:14:17 +0800
        Lines: 3
        X-Priority: 3
        X-MSMail-Priority: Normal
        X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
        X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
        X-RFC2646: Format=Flowed; Original
        """
        #[a-zA-z0-9]+@[a-zA-z0-9]+.[a-zA-z]+
        matchs = (
            ('From', r'From: (".+" <.+>)\r\n'),
            ('Newsgroups', r'Newsgroups: (.+)\r\n'),
            ('Subject', r'Subject: (.+)\r\n'),
            ('Date', r'Date: (.+)\r\n'),
            ('Lines', r'Lines: (.+)\r\n'),
            ('X-Priority', r'X-Priority: (.+)\r\n'),
            ('X-MSMail-Priority', r'X-MSMail-Priority: (.+)\r\n'),
            ('X-Newsreader', r'X-Newsreader: (.+)\r\n'),     
            ('X-MimeOLE', r'X-MimeOLE: (.+)\r\n'),  
            ('X-RFC2646', r'X-RFC2646: (.+)\r\n'),
            ('Xref', r'Xref: (.+)\r\n'),
            
            ('Body', r'\r\n\r\n(.+)\r\n\r\n\r\n') 
        )
        parse = {}
        for head, value in matchs:
            digs = re.compile(value,re.IGNORECASE)
            for test in digs.findall(data):
                parse[head] = test
#        print parse
        return parse

----- Original Message ----- 

From: lei zhenlin ray8866 at gmail.com>  

To: python-chinese at lists.python.cn 

Sent: Tuesday, July 04, 2006 9:56 AM

Subject: [python-chinese] 请教读取email的办法

 

大家好!  

我现需要解决这样一个问题:自动收取POP3邮件,将From、To、主题、信件内容保存到
数据库中,将附件保存到文件中,请问如何实现最方便?

我的初步思路是,尽量使用现有的软件,如在Linux下,收meil有fetchmail,收到的
mail存放在mbox邮箱中,如何解析每一封邮件就是我要实现的最关键问题了。在
windows下,我发现Thundbird邮件客户端的邮箱也是mbox格式的,所以可以用
Thundbird自动收邮件,但如何自动解析邮件也是关键所在。 

现在的邮件客户端都需要手工来查看邮件内容、另存附件等操作,不能满足我的自动解
析邮件内容的需求。

我查看了Python的mailbox、email等模块,发现是可以解析mbox邮箱的,但没有搞明白
到底如何实现。请教python-chinese的朋友们,是否能够帮助我解决一下这个问题呢?
非常感谢!

-- 
---------------------------------------- 
Lei zhenlin


  _____  


_______________________________________________
python-chinese
Post: send python-chinese at lists.python.cn
Subscribe: send subscribe to python-chinese-request at lists.python.cn
Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060704/c8fbc3f5/attachment-0001.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月04日 星期二 11:32

netkiller openunix at 163.com
Tue Jul 4 11:32:48 HKT 2006

Use-Case-用例实现规约:<用例名称>你用现有的库好些.性能要好很多.

我这段正则性能差.

我做的usenet 原码在我的svn上.

http://opensvn.csie.org/netkiller/Workspace/Python/src/
  ----- Original Message ----- 
  From: liguopeng 
  To: python-chinese at lists.python.cn 
  Sent: Tuesday, July 04, 2006 11:24 AM
  Subject: RE: [python-chinese] 请教读取email的办法


  我对这个比较有兴趣。不过python的经验比较少,呵呵。

  C++不错,MIME, POP3, SMTP等也比较熟。

   


------------------------------------------------------------------------------

  From: python-chinese-bounces at lists.python.cn [mailto:python-chinese-bounces at lists.python.cn] On Behalf Of netkiller
  Sent: 2006年7月4日 10:48
  To: python-chinese at lists.python.cn
  Subject: Re: [python-chinese] 请教读取email的办法

   

  正责.给你参考.我在写一个基于数据库的新闻组..你有兴趣吗?

   

  你的需求我很早以前做过..因为硬盘坏了,例子没了..你可以去网上找找我的贴子.

   

      def parseArticle(self,data):
          """
          From: "NEO" <openunix at 163.com>
          Newsgroups: cn.test
          Subject: test
          Date: Fri, 30 Jun 2006 17:14:17 +0800
          Lines: 3
          X-Priority: 3
          X-MSMail-Priority: Normal
          X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
          X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
          X-RFC2646: Format=Flowed; Original
          """
          #[a-zA-z0-9]+@[a-zA-z0-9]+.[a-zA-z]+
          matchs = (
              ('From', r'From: (".+" <.+>)\r\n'),
              ('Newsgroups', r'Newsgroups: (.+)\r\n'),
              ('Subject', r'Subject: (.+)\r\n'),
              ('Date', r'Date: (.+)\r\n'),
              ('Lines', r'Lines: (.+)\r\n'),
              ('X-Priority', r'X-Priority: (.+)\r\n'),
              ('X-MSMail-Priority', r'X-MSMail-Priority: (.+)\r\n'),
              ('X-Newsreader', r'X-Newsreader: (.+)\r\n'),     
              ('X-MimeOLE', r'X-MimeOLE: (.+)\r\n'),  
              ('X-RFC2646', r'X-RFC2646: (.+)\r\n'),
              ('Xref', r'Xref: (.+)\r\n'),
              
              ('Body', r'\r\n\r\n(.+)\r\n\r\n\r\n') 
          )
          parse = {}
          for head, value in matchs:
              digs = re.compile(value,re.IGNORECASE)
              for test in digs.findall(data):
                  parse[head] = test
  #        print parse
          return parse

    ----- Original Message ----- 

    From: lei zhenlin 

    To: python-chinese at lists.python.cn 

    Sent: Tuesday, July 04, 2006 9:56 AM

    Subject: [python-chinese] 请教读取email的办法

     

    大家好!  

    我现需要解决这样一个问题:自动收取POP3邮件,将From、To、主题、信件内容保存到数据库中,将附件保存到文件中,请问如何实现最方便?

    我的初步思路是,尽量使用现有的软件,如在Linux下,收meil有fetchmail,收到的mail存放在mbox邮箱中,如何解析每一封邮件就是我要实现的最关键问题了。在windows下,我发现Thundbird邮件客户端的邮箱也是mbox格式的,所以可以用Thundbird自动收邮件,但如何自动解析邮件也是关键所在。 

    现在的邮件客户端都需要手工来查看邮件内容、另存附件等操作,不能满足我的自动解析邮件内容的需求。

    我查看了Python的mailbox、email等模块,发现是可以解析mbox邮箱的,但没有搞明白到底如何实现。请教python-chinese的朋友们,是否能够帮助我解决一下这个问题呢?  非常感谢!

    -- 
    ---------------------------------------- 
    Lei zhenlin


----------------------------------------------------------------------------

    _______________________________________________
    python-chinese
    Post: send python-chinese at lists.python.cn
    Subscribe: send subscribe to python-chinese-request at lists.python.cn
    Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
    Detail Info: http://python.cn/mailman/listinfo/python-chinese



------------------------------------------------------------------------------


  _______________________________________________
  python-chinese
  Post: send python-chinese at lists.python.cn
  Subscribe: send subscribe to python-chinese-request at lists.python.cn
  Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
  Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060704/fcf8a4db/attachment-0001.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月04日 星期二 11:41

Ocean Chen chocean at gmail.com
Tue Jul 4 11:41:54 HKT 2006

解析邮件内容:
import mimetypes
for part in self.Mail.walk():
            # multipart/* are just containers
            if part.get_content_maintype() == 'multipart':
                WriteLog('Multipart mail')
                continue
            # Applications should really sanitize the given filename so that
an
            # email message can't be used to overwrite important files
            filename = part.get_filename()
            if not filename:
                WriteLog('No filename')
                try:
                    ext = mimetypes.guess_extension(part.get_type())
                except:
                    # Use a generic bag-of-bits extension
                    ext = '.txt'
                filename = 'part-%03d%s' % (counter, ext)
            counter += 1
            WriteLog('Save mail attachment to ' + os.path.join(self.SavePath,
filename))
            fp = open(os.path.join(self.SavePath, filename), 'wb')
            fp.write(part.get_payload(decode=1))
            fp.close()



在06-7-4,netkiller <openunix at 163.com> 写道:
>
>  你用现有的库好些.性能要好很多.
>
> 我这段正则性能差.
>
> 我做的usenet 原码在我的svn上.
>
> http://opensvn.csie.org/netkiller/Workspace/Python/src/
>
>  ----- Original Message -----
> *From:* liguopeng <liguopeng at tsinghua.org.cn>
> *To:* python-chinese at lists.python.cn
>  *Sent:* Tuesday, July 04, 2006 11:24 AM
> *Subject:* RE: [python-chinese] 请教读取email的办法
>
>
>
> 我对这个比较有兴趣。不过python的经验比较少,呵呵。
>
> C++不错,MIME, POP3, SMTP等也比较熟。
>
>
>  ------------------------------
>
> *From:* python-chinese-bounces at lists.python.cn [mailto:
> python-chinese-bounces at lists.python.cn] *On Behalf Of *netkiller
> *Sent:* 2006年7月4日 10:48
> *To:* python-chinese at lists.python.cn
> *Subject:* Re: [python-chinese] 请教读取email的办法
>
>
>
> 正责.给你参考.我在写一个基于数据库的新闻组..你有兴趣吗?
>
>
>
> 你的需求我很早以前做过..因为硬盘坏了,例子没了..你可以去网上找找我的贴子.
>
>
>
>     def parseArticle(self,data):
>         """
>         From: "NEO" <openunix at 163.com>
>         Newsgroups: cn.test
>         Subject: test
>         Date: Fri, 30 Jun 2006 17:14:17 +0800
>         Lines: 3
>         X-Priority: 3
>         X-MSMail-Priority: Normal
>         X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
>         X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
>         X-RFC2646: Format=Flowed; Original
>         """
>         #[a-zA-z0-9]+@[a-zA-z0-9]+.[a-zA-z]+
>         matchs = (
>             ('From', r'From: (".+" <.+>)\r\n'),
>             ('Newsgroups', r'Newsgroups: (.+)\r\n'),
>             ('Subject', r'Subject: (.+)\r\n'),
>             ('Date', r'Date: (.+)\r\n'),
>             ('Lines', r'Lines: (.+)\r\n'),
>             ('X-Priority', r'X-Priority: (.+)\r\n'),
>             ('X-MSMail-Priority', r'X-MSMail-Priority: (.+)\r\n'),
>             ('X-Newsreader', r'X-Newsreader: (.+)\r\n'),
>             ('X-MimeOLE', r'X-MimeOLE: (.+)\r\n'),
>             ('X-RFC2646', r'X-RFC2646: (.+)\r\n'),
>             ('Xref', r'Xref: (.+)\r\n'),
>
>             ('Body', r'\r\n\r\n(.+)\r\n\r\n\r\n')
>         )
>         parse = {}
>         for head, value in matchs:
>             digs = re.compile(value,re.IGNORECASE)
>             for test in digs.findall(data):
>                 parse[head] = test
> #        print parse
>         return parse
>
>  ----- Original Message -----
>
> *From:* lei zhenlin <ray8866 at gmail.com>
>
> *To:* python-chinese at lists.python.cn
>
> *Sent:* Tuesday, July 04, 2006 9:56 AM
>
> *Subject:* [python-chinese] 请教读取email的办法
>
>
>
> 大家好!
>
> 我现需要解决这样一个问题:自动收取POP3邮件,将From、To、主题、信件内容保存到数据库中,将附件保存到文件中,请问如何实现最方便?
>
> 我的初步思路是,尽量使用现有的软件,如在Linux下,收meil有fetchmail,收到的mail存放在mbox
> 邮箱中,如何解析每一封邮件就是我要实现的最关键问题了。在windows下,我发现Thundbird邮件客户端的邮箱也是mbox格式的,所以可以用
> Thundbird自动收邮件,但如何自动解析邮件也是关键所在。
>
> 现在的邮件客户端都需要手工来查看邮件内容、另存附件等操作,不能满足我的自动解析邮件内容的需求。
>
> 我查看了Python的mailbox、email等模块,发现是可以解析mbox邮箱的,但没有搞明白到底如何实现。请教python-chinese
> 的朋友们,是否能够帮助我解决一下这个问题呢?  非常感谢!
>
> --
> ----------------------------------------
> Lei zhenlin
>  ------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>  ------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


-- 
Ocean
mail: chocean at gmail.com
homepage: www.oceanisland.com.cn
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060704/818db8f5/attachment-0001.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月04日 星期二 11:49

shhgs shhgs.efhilt at gmail.com
Tue Jul 4 11:49:47 HKT 2006

py有专门的email模块

import email

messages = email.message_from_file(file('mbox'))

然后一个一个地iterate mail message

On 7/3/06, netkiller <openunix at 163.com> wrote:
>
>
> 你用现有的库好些.性能要好很多.
>
> 我这段正则性能差.
>
> 我做的usenet 原码在我的svn上.
>
> http://opensvn.csie.org/netkiller/Workspace/Python/src/
>
> ----- Original Message -----
> From: liguopeng
> To: python-chinese at lists.python.cn
>
> Sent: Tuesday, July 04, 2006 11:24 AM
> Subject: RE: [python-chinese] 请教读取email的办法
>
>
>
>
> 我对这个比较有兴趣。不过python的经验比较少,呵呵。
>
> C++不错,MIME, POP3, SMTP等也比较熟。
>
>
>
>  ________________________________
>
>
> From: python-chinese-bounces at lists.python.cn
> [mailto:python-chinese-bounces at lists.python.cn] On Behalf
> Of netkiller
> Sent: 2006年7月4日 10:48
> To: python-chinese at lists.python.cn
> Subject: Re: [python-chinese] 请教读取email的办法
>
>
>
>
> 正责.给你参考.我在写一个基于数据库的新闻组..你有兴趣吗?
>
>
>
>
>
> 你的需求我很早以前做过..因为硬盘坏了,例子没了..你可以去网上找找我的贴子.
>
>
>
>
>
>     def parseArticle(self,data):
>         """
>         From: "NEO" <openunix at 163.com>
>         Newsgroups: cn.test
>         Subject: test
>         Date: Fri, 30 Jun 2006 17:14:17 +0800
>         Lines: 3
>         X-Priority: 3
>         X-MSMail-Priority: Normal
>         X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
>         X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
>         X-RFC2646: Format=Flowed; Original
>         """
>         #[a-zA-z0-9]+@[a-zA-z0-9]+.[a-zA-z]+
>         matchs = (
>             ('From', r'From: (".+" <.+>)\r\n'),
>             ('Newsgroups', r'Newsgroups: (.+)\r\n'),
>             ('Subject', r'Subject: (.+)\r\n'),
>             ('Date', r'Date: (.+)\r\n'),
>             ('Lines', r'Lines: (.+)\r\n'),
>             ('X-Priority', r'X-Priority: (.+)\r\n'),
>             ('X-MSMail-Priority', r'X-MSMail-Priority: (.+)\r\n'),
>             ('X-Newsreader', r'X-Newsreader: (.+)\r\n'),
>             ('X-MimeOLE', r'X-MimeOLE: (.+)\r\n'),
>             ('X-RFC2646', r'X-RFC2646: (.+)\r\n'),
>             ('Xref', r'Xref: (.+)\r\n'),
>
>             ('Body', r'\r\n\r\n(.+)\r\n\r\n\r\n')
>         )
>         parse = {}
>         for head, value in matchs:
>             digs = re.compile(value,re.IGNORECASE)
>             for test in digs.findall(data):
>                 parse[head] = test
> #        print parse
>         return parse
>
>
>
> ----- Original Message -----
>
>
> From: lei zhenlin
>
>
> To: python-chinese at lists.python.cn
>
>
> Sent: Tuesday, July 04, 2006 9:56 AM
>
>
> Subject: [python-chinese] 请教读取email的办法
>
>
>
>
> 大家好!
>
> 我现需要解决这样一个问题:自动收取POP3邮件,将From、To、主题、信件内容保存到数据库中,将附件保存到文件中,请问如何实现最方便?
>
> 我的初步思路是,尽量使用现有的软件,如在Linux下,收meil有fetchmail,收到的mail存放在mbox邮箱中,如何解析每一封邮件就是我要实现的最关键问题了。在windows下,我发现Thundbird邮件客户端的邮箱也是mbox格式的,所以可以用Thundbird自动收邮件,但如何自动解析邮件也是关键所在。
>
> 现在的邮件客户端都需要手工来查看邮件内容、另存附件等操作,不能满足我的自动解析邮件内容的需求。
>
> 我查看了Python的mailbox、email等模块,发现是可以解析mbox邮箱的,但没有搞明白到底如何实现。请教python-chinese的朋友们,是否能够帮助我解决一下这个问题呢?
>  非常感谢!
>
> --
> ----------------------------------------
> Lei zhenlin
>  ________________________________
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>  ________________________________
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号