One rule I try to code by is to offload all the drugery to modules, so that I focus as much as possible on a higher level. The more you offload to modules, the shorter and clearer your code gets.

On that note, a few of your subroutines could be replaced by CPAN modules, which will shorten your code and free your focus.

Before I get into that, one question: Is it possible for you to filter and redirect the mail in real-time as they come in? If so, then you should look into Mail::Audit, which allows you to parse and filter mail pretty easily. Using this method should simplify the process significantly.

Ok, back to the code. The first thing I would do is look at your getDate() subroutine. Check out Date::Format which will format the date in the exact specification you desire, with much less code to maintain.

Next, you could replace the check_args() and usage() with Getopt::Declare or a combination of Getopt::Std and Pod::Usage. I prefer Getopt::Declare as it's argueably simpler to use and does what I want.

You can replace parse_head() with some of the MailTools modules. For example, the output from the Mail::POP3Client methods will be perfect input into Mail::Header and Mail::Internet's new() constructors. From there you can just use method calls to get at the From, To, Subject and Body of the message. No more parsing of the email messages with regexs, it's all done for you, very reliably.

Also, in your initial loop, you save a copy of the incoming message to a log file, I presume. The way you are opening up the file handle, using >, will clobber the file each time you write to it. My guess is that you need to use >>, which tells open() that you'd like to append to the file, not overwrite it.

At first glance you're probably thinking "this is alot of stuff to learn". That's true, but I would argue that, at some point, learning CPAN modules is almost as important as learning the core perl commands. At first it seems like alot, but every time you use a new module, the code you got working serves as a reference for next time. After a short period of time you have your own personal reference library and the time to implement similar tasks drops considerably.

Hope this helps.


In reply to Re: Mail::Sendmail by dkubb
in thread mail::sendmail with an array of hashes by dystrophy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.