#! /usr/bin/perl # # Program: MailIt.pl # Author: Jack Coxen # # Orig: April 18, 2005 # Purpose: Generic script to parse a file (given on the command line) for: # addr@wherever.com # addr2@wherever.com (optional field) # What's it all about, Alfie? # Whatever text you want included in the body of your # email. Text formatting will (probably) be preserved. # /whatever/path/you/need/to/get/to/your/file.xls # # NOTE: Multiple email addresses can be used in the and # 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 section will show up as clickable # links if your email client supports it. Also, email addresses will # be clickable if preceded by a 'mailto:'tag (i.e. mailto: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 development ONLY!!! # Other use statements use MIME::Lite; # Low-calorie MIME generator use MIME::Types; # Definition of MIME types use Config::Tiny; # Read/Write .ini style 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 while () { $tmp .= $_; } if ($tmp =~ /(.*)<\/to>/) { $to = $1; } if ($tmp =~ /(.*)<\/cc>/) { $cc = $1; } else { $cc = ""; } if ($tmp =~ /(.*)<\/subject>/) { $subject = $1; } else { $subject = ""; } if ($tmp =~ /(.*)<\/msgbody>/s) { $msgbody = $1; } else { $msgbody = ""; } if ($tmp =~ /(.*)<\/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;