Sending Mails with Perl and sendmail

April 2, 2009 under Perl, Programming

If you have to code a script, which has to  send mails from an system, where sendmail is installed,  here is an very simple code-snipled, which you can use.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/perl -w
# Snippled to send Mails via sendmail
# by www.thedigitalchaos.de
 
# Recipient (replace sender@domain.tld whith the Recipients E-Mail-Adress)
my $mailto = 'someone@domain.tld';
 
# Sender (replace sender@domain.tld whith the Senders E-Mail-Adress)
my $from = 'sender@domain.tld';
 
# Subject
my $subject = "The Subject";
 
# The Content Smile
my $mailtext = " Text line1\n";
$mailtext .= " Text line2\n";
$mailtext .= " Text line3\n";
$mailtext .= " Text line4\n";
$mailtext .= " Text line5\n";
 
# sending the mail 
# (if the sendmail binary is installed in another directory, 
# you must change the path at the next line)
open(MAIL, "|/usr/lib/sendmail -t");
print MAIL "To: $mailto\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject \n\n";
print MAIL "$mailtext";
close(MAIL);

Click here to read more.. »

Share
comments: 0 » tags: , , , ,

Recursive file search with Perl

March 2, 2009 under Perl

If you have to search a file in a directory tree with Perl, just use the following example:

Search a file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/perl
# Code Snipplet to search a file
# by www.thedigitalchaos.de
#
use strict;
 
# show no warnings about recursion (we know what we do Wink )
no warnings "recursion";
 
# specify the file you search here (in this example "access_log" ) :
my $file = "access_log";
 
# specify the directory where you want to start the search (in this example "/var" ) :
my $searchdir = "/var";
 
# Calling the Subroutine, which searches the File
readDirectory($searchdir, $file);
 
# We need an Subroutine, which can be called on every sub-directory
sub readDirectory
{
     my $searchdir = shift;
     my $searchfile = shift;
 
     # a little bit output, in which directory the script
     # is searching at the moment (the following line is not necessary)
     print "Searching in $searchdir n";
 
     # Open and close the directory
     opendir DIR, $searchdir or die("An error occured: $!");
     my @files = readdir(DIR);
     closedir DIR;
 
     foreach my $currentFile (@files)
     {
          # In Unix/Linux we have the directorys "." and "..",
          # it's no good idea to scan these, so let them skip.
          next if $currentFile =~ /^\./;
 
          # Lets have a look, if the current "file" is the searched file,
          # else have a look, if the "file" is an directory,
          # and if its one, lets have a look, if the searched file is into it.
          if ( $currentFile eq $searchfile )
          {
               # We found the right file, now we can do somthing with it,
               # in this case, we only print a text
               print "Found the file: $searchdir/$currentFile n";
          }
          if ( -d "$searchdir/$currentFile" )
          {
               # The Subroutine is calling hisself with the new parameters
               readDirectory("$searchdir/$currentFile", $searchfile);
          }
     }
}

How does the skript work:
Click here to read more.. »

Share
comments: 2 » tags: , , ,