FAQ 9.15 How do I parse a mail header?

Discussion in 'Perl Misc' started by PerlFAQ Server, Apr 10, 2011.

  1. This is an excerpt from the latest version perlfaq9.pod, which
    comes with the standard Perl distribution. These postings aim to
    reduce the number of repeated questions as well as allow the community
    to review and update the answers. The latest version of the complete
    perlfaq is at http://faq.perl.org .

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

    9.15: How do I parse a mail header?

    For a quick-and-dirty solution, try this solution derived from "split"
    in perlfunc:

    $/ = '';
    $header = <MSG>;
    $header =~ s/\n\s+/ /g; # merge continuation lines
    %head = ( UNIX_FROM_LINE, split /^([-\w]+):\s*/m, $header );

    That solution doesn't do well if, for example, you're trying to maintain
    all the Received lines. A more complete approach is to use the
    "Mail::Header" module from CPAN (part of the "MailTools" package).



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

    The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
    are not necessarily experts in every domain where Perl might show up,
    so please include as much information as possible and relevant in any
    corrections. The perlfaq-workers also don't have access to every
    operating system or platform, so please include relevant details for
    corrections to examples that do not work on particular platforms.
    Working code is greatly appreciated.

    If you'd like to help maintain the perlfaq, see the details in
    perlfaq.pod.
    PerlFAQ Server, Apr 10, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    19
    Views:
    1,105
    Daniel Vallstrom
    Mar 15, 2005
  2. Baron Samedi

    Seeking code to parse header files

    Baron Samedi, Aug 16, 2007, in forum: C++
    Replies:
    1
    Views:
    327
    Alan Woodland
    Aug 16, 2007
  3. mlt
    Replies:
    2
    Views:
    817
    Jean-Marc Bourguet
    Jan 31, 2009
  4. Philip Semanchuk

    Re: How to parse HTTP time header?

    Philip Semanchuk, Nov 8, 2009, in forum: Python
    Replies:
    0
    Views:
    711
    Philip Semanchuk
    Nov 8, 2009
  5. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    175
    7stud --
    Feb 20, 2008
Loading...

Share This Page