Some months back I needed a script to do the same thing...and I took a similar approach using MIME::Lite. Here's what I came up with.
#! /usr/bin/perl
#
# Program: MailIt.pl
# Author: Jack Coxen <Jack.Coxen@TelCove.com>
#
# Orig: April 18, 2005
# Purpose: Generic script to parse a file (given on the command l
+ine) for:
# <to>addr@wherever.com</to>
# <cc>addr2@wherever.com</cc> (optional field)
# <subject>What's it all about, Alfie?</subject>
# <msgbody>Whatever text you want included in the bo
+dy of your
# email. Text formatting will (probably) be pre
+served.</msgbody>
# <attach>/whatever/path/you/need/to/get/to/your/fil
+e.xls</attach>
#
# NOTE: Multiple email addresses can be used in the <to>
+</to> and <cc></cc>
# fields by seperating them with a comma
# (e.g. user1@address1.com, user2@address2.com)
#
# NOTE: Multiple files can be attached by seperating the
+ filenams with a colon
# (e.g. /some/path/file1.txt:/some/other/path/file2.
+gif:/tmp/stuff)
#
# NOTE: URLs included in the <msgbody></msgbody> section
+ will show up as clickable
# links if your email client supports it. Also, ema
+il addresses will
# be clickable if preceded by a 'mailto:'tag (i.e. m
+ailto:user1@address1.com)
#
# Last Modified on: May 6, 2005
# Last Modified by: Jack Coxen
# What was done: Modified to allow multiple attachments
#
# Default use statements
use strict;
use warnings;
#use diagnostics; # Uncomment this for d
+evelopment ONLY!!!
# Other use statements
use MIME::Lite; # Low-calorie MIME gen
+erator
use MIME::Types; # Definition of MIME t
+ypes
use Config::Tiny; # Read/Write .ini styl
+e files with as little code as possible
# Set DEBUG Messages on or off
my $DEBUG = 0;
# Setup variables
my $config = Config::Tiny->read( '/usr/local/config/PerlConfig' )
+;
my $admin = $config->{People}->{admin};
my $mailsvr = $config->{Servers}->{mailsvr};
my $to;
my $cc;
my $subject;
my $msgbody;
my $attach = 0;
my $attachment;
my $tmp;
my $buf;
#
# Main Program
#
open MAILFILE, "<$ARGV[0]"
or die "Can't open file: $!\n";
# Pull the various parts of the message out of <MAILFILE>
while (<MAILFILE>) {
$tmp .= $_;
}
if ($tmp =~ /<to>(.*)<\/to>/) {
$to = $1;
}
if ($tmp =~ /<cc>(.*)<\/cc>/) {
$cc = $1;
} else {
$cc = "";
}
if ($tmp =~ /<subject>(.*)<\/subject>/) {
$subject = $1;
} else {
$subject = "";
}
if ($tmp =~ /<msgbody>(.*)<\/msgbody>/s) {
$msgbody = $1;
} else {
$msgbody = "";
}
if ($tmp =~ /<attach>(.*)<\/attach>/) {
$attachment = $1;
$attach = 1;
}
if ($DEBUG) {
print "Filename = ".$ARGV[0]."\n";
print "Recipient = ".$to."\n";
print "cc = ".$cc."\n";
print "Subject = ".$subject."\n";
print "Body = ".$msgbody."\n";
print "Attachment = ".$attachment."\n" if ($attach);
print "End DEBUG section\n";
}
# Assemble the Email message
my $msg = MIME::Lite->new(
FROM => 'Capacity Administraot',
To => $to,
Cc => $cc,
Subject => $subject,
Type => 'multipart/mixed',
);
$msg->attach(
Type => 'TEXT',
Data => $msgbody
);
if ($attach) {
my @attachments = split (/:/,$attachment);
my $num = @attachments;
print "Num = $num\n" if $DEBUG;
while ($num > 0) {
$num -= 1;
print "Attachment = $attachments[$num]\n" if $DEBUG;
print "Num = $num\n" if $DEBUG;
$msg->attach(
Type => 'AUTO',
Path => $attachments[$num]
);
}
}
# Send the Email message
MIME::Lite->send('smtp', $mailsvr, Timeout=>60);
$msg->send;
exit;
I don't doubt that the code can be simplified and cleaned up considerably but this works well enough for me. I tend to write code with the assumption that it'll have to be maintained by someone who's still learning Perl - kinda like me. Anyway, if you find it useful you're welcome to use it.
Jack