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

Hi.. Still writting the "free e-mails" script, I'm in the delivery part.

I have a script that gets all the mail from STDIN, that delivers the mail to a mysql database. I have 2 questions/problems with the attachments:

- I found a lot of modules with nice examples that show how to create and send an e-mail with an attachment, but I can't find anything to get get the mime parts and save them on files or something. The closest I found was Mime::parser, but I don't understan the documentation.. :) Can anyone suggest somthing, or post examples?
- Attachments could be really big, so I don't want to use the script's memory to hold the file. I think the modules send everything to a temp file, but how would I send that temp file to the database without storing everithing on a temp. variable? (is there a "binary" data type on mysql, or something like that?)

Thanks!

Replies are listed 'Best First'.
Re: dealing with attachments (again)
by httptech (Chaplain) on May 25, 2000 at 21:09 UTC
    Look at my snippet for extracting MIME attachments, it may be helpful.

    While you could store binary data in MySQL, I can't think of a good reason to do so. If the attachments are so big you don't want to store them in memory, they will probably be too big to move in and out of MySQL also.

    Use my snippet to save the attachments to the server, then save the path to MySQL instead.

      I second that. You would be increasing your performance by a great deal just by finding a clever way to keep the attachments directly on the filesystem. I would suppose that you have a user primary key and a message primary key, correct? If so, you could try doing a directory tree like:
      /users/ /user1/ /attachmenta /attachmentb /attachmentc /user2/ /attachmenta /attachmentb /attachmentc
      Where userx would be the user pk and attachmentx would be the attachment pk (doh!). On the downside, it would probably be nice to develop a tool or cron job to keep track of lost attachments, old stuff and so forth...

      #!/home/bbq/bin/perl
      # Trust no1!
        Well, my main concerns were:

        - I need to be able to figure out the ammount of disk space that a user is using, in a fast and easy way.
        - There is a security problem with saving the attachments on a public_html directory.
        - (this is the most important) I'd like to have all the database in one place.

        So there is no way to send the content of a _big_ file to mysql without using a lot of ram?

        Thanks!

      Hi..

      1 question about the code: what happends if the file alredy exists on the temp. directory? it looks like the mime::parser object is creating the file.. how do I get the object not to overwrite the existing file?

      Thanks..

        Yes, it does overwrite existing files. You could probably change this behavior by writing a subclass for Mime::Parser to override some of the built-in functions it uses where the filename is determined, but that's beyond the scope of a snippet I think.
Re: dealing with attachments (again)
by dfrazier (Initiate) on May 25, 2000 at 21:18 UTC
    check out Mime::Lite.
      Also be aware some some mail clients decide to throw standards to the wind and can/will encode attachments as uuencoded strings instead of MIME parts. In particular, I've had to fix client machines running Outlook Express which were set improperly (for no good reason) by the user to uuencode. But you can still handle these just fine with Convert::UU;