Awhile back, I needed a script to send an alert email with file attachments... yes multiple attachments. Being that Perl is my weapon of choice for most scripting needs and I've used Mail::Sendmail in the past I set out to make this happen. The trouble I found is that the most common examples of sending attachments using Mail::Sendmail only show how to attach one file. To complicate matters, most examples love to use perl's special block printing functions to accomplish this. It makes understanding what the examples are doing with MIME types very difficult. Well, I waded through it, trying a change here and a change there and I have compiled an example that sends an email with multiple attachments using Mail::Sendmail.

Essentially, there is a hash called %files where the keys are the file names and the value holds the contents of the file. Any file in the hash gets appended as an attachment. (Warning this is not a good way to go if your files are LARGE, mine aren't, but the example should be fairly straight forward and you can easily modify this code to handle reading the file from disk while constructing the email. ) The $message variable makes up the body of the email. In this case it is an HTML message - it could be plain text, whatever. See the Mail::Sendmail documentation for information on the remaining variables - they are all standard.

I hereby release this example code into the public domain, without waranty or implied waranty or guarantee of fitness of any kind. Cheers.

 

Add to My Yahoo!

Add to Google

Subscribe with Bloglines

Austin Gilbert/Male/26-30. Lives in United States/Oklahoma/Tulsa/Midtown, speaks English. Spends 40% of daytime online. Uses a Fast (128k-512k) connection. And likes computer science/photography.
This is my blogchalk: United States, Oklahoma, Tulsa, Midtown, English, Austin Gilbert, Male, 26-30, computer science, photography.

Using Perl::Sendmail to send multiple MIME attachments
2006/10/17