Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm viewing the contents of my mail box using Net::POP3. I would like to keep this box clean of mails with attachments, by simply establishing whether an attachment exists and deleting the email. I'm using the code outlined by Mr Barr to read the mails so unless there is great demand I won't reproduce it here. However on searching the pm site I see that to deal with attachments it is recommended to use email::mime, which is not currently provided on my server. I shall probably ask for it to be installed but in the meantime is there a line I can search for in the mail to tell me consistently if the attachment exists? It would seem to be overkill to use a module, although if it's the easiest, foolproof way then so be it...
  • Comment on establishing the existance of an email attachment

Replies are listed 'Best First'.
Re: establishing the existance of an email attachment
by Celada (Monk) on Dec 06, 2005 at 07:09 UTC

    Yeah, you probably really want to be using a module to parse the MIME structure of the message, but you can probably still do some quick and dirty things.

    You should search for Content-Type: headers. You can do something fancy or something crude. Something crude would be to catch it wherever it appears in the message, but this means you would catch it even if it appears as part of the text of the message (well, how likely is that??). You can get fancy and try to catch it only when it appears in MIME body part headers and not in bodies themselves, but the fancier you get the close you get to reinventing a preexisting MIME parser module such as the one you suggest -- so try to get one installed instead :-)

    Once you are collecting Content-Type:s, you should whitelist the ones you will accept:

    • message/*, because this forms part of the MIME structure and does not introduce a body part directly.
    • multipart/*, same reason
    • text/*, because these are generally the body parts that are not considered "attachments".

    Anything else, like image/* and application/* is likely to be what should be considered an attachment.

    Once you get a proper MIME parsing module installed and begin using it, you can use the same technique of whitelisting text/* but you won't have to worry about message/* and multipart/* because those will probably be dealt with by the module.

    There is another header, called Content-Disposition: which specifies unambiguously whether a body part is inline or is an attachment, but you probably cannot use that because it is not always present.

Re: establishing the existance of an email attachment
by duff (Parson) on Dec 06, 2005 at 06:09 UTC

    Probably not foolproof, but you can check the Content-Type: header. If it contains multipart/mixed then you have attachments. (At least this is what a quick perusal of my email with attachments seems to indicate)

Re: establishing the existance of an email attachment
by svenXY (Deacon) on Dec 06, 2005 at 06:45 UTC
    Hi,
    I would assume that one can create a mail with a mulitpart/mixed header without an attachment, therefore I would at least take the size of the mail into consideration as well.
    Regards,
    svenXY