Friday, September 25, 2009

PERL script to test SMTP connections

SMTP CHECK PERL SCRIPT:
USAGE:- ./perlscript smtp servername

#!/usr/bin/perl

use Net::SMTP;

$filename = $ARGV[0];
$smtpserver = $ARGV[1];
open (FH, "<$filename") || die "Could not open email file $!\n";
(@lines) = ; # read file into list
close(FH);

#open(EFILE2,"<$text_attach") || die "Could not open email file $!\n";
#(@lines) = ; # read file into list
#close(EFILE2);
#close(FH);

#my $ServerName = $smtpserver;
#$smtp = Net::SMTP->new($ServerName, Debug => 0);
#$smtp = Net::SMTP->new("xx.xx.xx");

$smtp = Net::SMTP->new($smtpserver,
Hello => 'mail.testmail.co.in',
Port => 25,
Timeout => 30,
Debug => 1,
);


$from="test\@rediffmail.com";
$cc_list="test\@yahoo.com";
#$recip2="f\@lehman.com";
$recip2="test\@anilkumar.com";


$smtp->mail("$from");

###############################################
##$smtp->to("$recip,$cc_list");
##$smtp->cc("$cc_list");
##$smtp->bcc("$bcc");
###############################################

$smtp->recipient($recip2,$cc_list);
$smtp->data;

$smtp->datasend("To: $recip2\n");
$smtp->datasend("From: $from\n");

$smtp->datasend("Cc: $cc_list\n");
$smtp->datasend("Bcc: $bcc\n");
$smtp->datasend("Subject: $subject\n");

$smtp->datasend("MIME-Version: 1.0 \n");
$smtp->datasend("Content-Type: text/html; charset=us-ascii \n");
$smtp->datasend("\n");

$smtp->datasend(@lines);

$smtp->dataend();
$smtp->quit;

Perl script to uninstall Modules Cleanly

This script detects the installed perl modules:-


#!/usr/local/bin/perl -w

use strict;
use IO::Dir;
use ExtUtils::Packlist;
use ExtUtils::Installed;

sub emptydir($) {
my ($dir) = @_;
my $dh = IO::Dir->new($dir) || return(0);
my @count = $dh->read();
$dh->close();
return(@count == 2 ? 1 : 0);
}

# Find all the installed packages
print("Finding all installed modules...\n");
my $installed = ExtUtils::Installed->new();

foreach my $module (grep(!/^Perl$/, $installed->modules())) {
my $version = $installed->version($module) || "???";
print("Found module $module Version $version\n");
print("Do you want to delete $module? [n] ");
my $r = ; chomp($r);
if ($r && $r =~ /^y/i) {
# Remove all the files
foreach my $file (sort($installed->files($module))) {
print("rm $file\n");
unlink($file);
}
my $pf = $installed->packlist($module)->packlist_file();
print("rm $pf\n");
unlink($pf);
foreach my $dir (sort($installed->directory_tree($module))) {
if (emptydir($dir)) {
print("rmdir $dir\n");
rmdir($dir);
}
}
}
}