Sending Email with Perl

envelope Sending Email with PerlRecently I needed to send our team an email.  Yeah okay, so I email a lot, but this was different.  I needed to send my team an email from a server that was not anywhere near a mail server, and that was pretty paired down.  I did a bunch of Googling for answers and came up with quite a few that didn’t work.

I hate it when that happens.  So as a form of perl tutorial, I am posting my working script here as an example of sending email from a limited server using perl and the MIME::Lite library.

  • Server:  CentOS 5.2
  • Internet connection: Present
  • Task:  Automation Scripts written in Perl

So I had perl, but how to send an email.  Well the first thing, is get the mail libraries.  On a Red Hat based server, this is yum install perl-MIME-tools.  You may have a different way.  But this is required for the sending messages in MIME.  (if you can’t install perl and perl modules, you may want to use CPAN)

Then I wrote this perl script:

—-begin code  —-

#!/usr/bin/perl
#declare that you are using the MIME library at the top.
use MIME::Lite;

#list files in /Email into array
opendir MYDIR, “/share/Email/” or die “couldn’t open $! n”;

#This will only grab files with “PDF” in their name.

#this will eliminate other files, and more importantly, “.” and “..” that are directories.
@contents = grep(/pdf/,readdir (MYDIR));
closedir MYDIR;

#we want to know how many files are in our list.
$fileCount = @contents;

#loop array to sendmail function
for ($i = 0; $i < $fileCount; ++$i){
$fileName = “/share/Email” . $contents[$i];
$Subject = “Alert Request for: ” . $contents[$i];
#print $fileName . “n”;

my $msg = MIME::Lite->new(
From    => ‘mail@yourserver.com’,
To    => ‘roy@TheIntegrationEngineer.com’,
Cc    => ”,
Subject    => $Subject,
Type    => ‘multipart/mixed’,
);

$msg->attach(
Type    => ‘Text’,
Data    => ‘Please read this file’,
);

$msg->attach(
Type    => ‘Data’,
Path    => $fileName,
Filename    => $contents[$i],
);

$msg->send(’smtp’, ‘mail.yourserver.com’);

#Remove File
$cmd = “mv ” . $fileName . ” /alert/backup/” . $filename . “.” . epoc . ” n”;
print $cmd;
system ($cmd);
}

—- end code —-

Summary

Basically this script polls the specified directory for PDF files.  If it finds one or more it grabs each of them and emails them to me.  Then, as a last step, it moves the file to a backup location to be archived.

There are probably other ways to do this, but this is a script that I threw together and have working on one of my severs.  If anyone want to add some corrections to this I will try them out and post my results.

Subscribe to "The Integration Engineer" by Email
Find out about the tools and services available at The Integration Engineer's Consulting site.

Related Articles:

One Response to “Sending Email with Perl”

  1. Drew Says:

    As good practice with perl I’d strongly suggest two friends:

    use strict;
    use warnings;

    And depending on how the code is to be triggered Taint checking may also be prudent.

    Other perl tools I find helpfull:
    perltidy, perlcritic

    Lastly…. smartquotes? Ghastly.

Leave a Reply

  • Sign up for our FREE Newsletter

  • Catagories


  • Affiliate Ads