#!/usr/bin/perl use strict; use warnings; use MIME::Lite; use File::Copy qw( move ); use File::Basename; fileparse_set_fstype($^O); # Specify OS type. my $suffix_pattern = qr/\.[^.]*/; my $mime_types = { '.txt' => 'text/plain', '.jpg' => 'image/jpeg', '.gif' => 'image/gif', '.zip' => 'application/zip', '.doc' => 'application/msword', '.docx' => 'application/msword', '.xls' => 'application/vnd.ms-excel', '.xlsx' => 'application/vnd.ms-excel', }; my $mime = MIME::Lite->new( 'To' => 'me@example.com', 'Cc' => 'you@example.com,them@example.com', 'From' => '"Us" ', 'Subject' => 'Email using Perl', 'Type' => 'text/plain', 'Data' => 'Text of email message goes here', ); my $dir = "/tmp"; opendir(DIR, $dir) or die "Error opening directory: $dir\n"; print "\nFormatting email ... \n"; print "\nAttaching files: "; my ($full_name_old, $full_name_new); while (my $file_name = readdir(DIR)) { my $full_name = "$dir/$file_name"; next if $file_name =~ /^\.\.?/; # Skip 'dot' files. next if $file_name =~ /.bat$/; # Skip batch files. next if $file_name eq "Huge.zip"; # File exceeds email max. next if -d $full_name; # Skip directories. $full_name_old = $full_name_new = ''; if ($file_name =~ /\.pl$/) { # Add txt extension for email filters. $file_name .= '.txt'; $full_name_old = $full_name; $full_name_new = $full_name . '.txt'; $full_name = $full_name_new; move($full_name_old,$full_name_new); } print "$file_name, "; my ($file, $path, $ext) = fileparse($full_name, $suffix_pattern); my $mime_type = $mime_types->{$ext} || 'text/plain'; $mime->attach( Type => $mime_type, Path => $full_name, Filename => $file_name, Disposition => 'attachment', ); } closedir(DIR); print "\n\nSending email ... "; MIME::Lite->send( 'smtp', 'smtp.yourisp.net', 'Timeout' => 90 ); $mime->send; opendir(DIR, $dir) or die "Error opening directory: $dir\n"; while (my $file_name = readdir(DIR)) { my $full_name = "$dir/$file_name"; if ($full_name =~ /\.pl\.txt$/) { # Remove txt extensions added above. $full_name_old = substr($full_name,0,length($full_name)-4); move($full_name,$full_name_old); } } closedir(DIR); print "\n\nFiles in '$dir' sent to recipients as attachments\n";