[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] My first script



Well it's essentially finished, so I thought I'd share.  I'm sure it
could be improved, but honestly I'm just impressed I got it to work.
Now I just need to tweak it to work in unattended cron mode.

I sanitized the code so none of my particulars are there, for obvious
reasons.  Big thanks to Jay for pointing me towards the Email::Stuff
module.

Bill

***************************************************************************************************************

#!/usr/bin/perl


use strict;
use Net::LDAP;
use IO::File;
use Email::Stuff;

#Creates connection to netusrA on the LDAP port, using the ldap_binder account
my $netad = Net::LDAP->new("myhost.mydomain") or die "Could not connect! $!";
$netad->bind("bind account here", password=>"bind account password here");

#Defines the LDAP search to start at the root of the directory tree
my $base = 'root of my LDAP directory tree';

#The filter is in LDAP format
#The filter specifies users that have an SN (last name) that exists,
but is not equal to System nor starts with Sharepoint
#AND that have an email address
#AND that have a givenname (first name) that is not equal to NET
my $filter = "(&(&(!(sn=System))(!(sn=Sharepoint*))(sn=*))(mail=*)(&(!(givenname=NET))(givenname=*)))";

# $attrs specifies which attributes to be retrieved
my $attrs = ["givenname","sn","mail"];

# This creates a search result object ($results) from the netad search method
my $results = $netad->search(base=>$base,filter=>$filter,attrs=>$attrs);

# This uses the count method of $results to return the number of
records matching the filter
my $count = $results->count;



#message ("Records returned $count");



#Creates an output file named emailList.csv or returns error.
my $outputfile = IO::File->new("emailList.csv","w") or die "Could not
open output file $!";

#Loop to write each result line to the output file
for (my $i=0;$i<$count;$i++){
	
	#Assigns the result at $i to the variable $foo
	my $foo = $results->entry($i);
		#Assigns the attribute names to variables
		my $sn="sn";
		my $gn="givenName";
		my $mail="mail";
		
		#Assigns the value from $foo to a specific variable
		my $lastname = $foo->get_value($sn);
		my $firsttname = $foo->get_value($gn);
		my $email = $foo->get_value($mail);
		
		#Concatenates the variables to a csv output line.  The \n is the new
line operator
		my $line = "$firsttname,$lastname,$email\n";
		
#  Writes the same output to the screen		
#		message ("$line");

		#Writes the data line to the output file
		$outputfile->print($line);

}

#Closes the output file
undef $outputfile;

#Closes the LDAP connection
$netad->unbind;

#Email the output file to the SharePoint doc library using the SMTP server
Email::Stuff->To('destination e-mail')
			->from('source e-mail')
			->Subject("Automatically generated e-mail list")
			->text_body("E-mail list for Communications department.  Used for
Constant Contact service.")
			->attach_file('emailList.csv')
			->using('SMTP',Host => 'my mail server')
			->send;


sub message
{
    my $m = shift or return;
    print("$m\n");
}