LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-14-2004, 12:07 AM   #1
Bladez
LQ Newbie
 
Registered: Aug 2003
Location: Massachusetts
Distribution: Mandriva 2006, Ubuntu 6.06
Posts: 27

Rep: Reputation: 15
Mail script with Perl & Postfix


Greetings everyone,

I have gotten a little stumped as I am not too familiar with Postfix. I took a perl script from a recent issue of 2600 magazine. (Vol20, #2) It was designed for IIS 5, but I wanted the mailing part of it for my own script.

Here is my scenario. The script creates a .TXT file in the outgoing mail queue directory. I assume that from there it gets sent out. My problem is that I don't know where to set the path of that mail queue.

As the script is not long, I will put it up here, and any suggestions would be great!

Code:
#!/usr/bin/perl5.8.1
# Modification of Watcher.cgi from 2600 - Vol 20, 2

$recipient = "admin\@mynetwork.com";
@cclist = ("me\@mynetworks.com");
#$smtp_server_name = "localhost";
$smtp_pickup_path = "\/var\/spool\/mail\/";

#&getIP;
&getDateTime;
&writeMail;

sub getDateTime {
                my ($sec,$min,$hour,$mday,$mon,$year,$wday) = gmtime((time-18000));
                my $ampm;
                my $hrformat;
                $year = 1900 + $year;
                if ($min < 10) { $min = " 0$min"; }
                if ($sec < 10) { $sec = "0$sec"; }
                if ($hour < 12) { $ampm = "am"; } else { $ampm = "pm"; }
                if ($hour > 12) { $hour = ($hour - "12");}
                if ($hour eq "0") { $hour = "12";}
                $datetime = ($mon + 1) . "\/$mday\/$year at $hour:$min $ampm EST";
}

# The next line is to test the getDateTime function
print "$datetime\n";

sub writeMail {
                my $server_name = lc($ENV{'COMPUTERNAME'});
                my $from_name = "$server_name M3IP";
                my $from_email = "$server_name\@x12networks.com";
                my $subject = "Updated IP";
                $tempfile = "m3ip\-out";

                open TMP, ">>$smtp_pickup_path$tempfile" or die;
                print TMP "x-sender: $from_email\n";
                print TMP "x-receiver: $recipient\n";
                if (@cclist) {
                foreach my $ccaddress (@cclist) {
                print TMP "x-receiver: $ccaddress\n"; }
                }
                print TMP "To: $recipient\n";
                print TMP "CC: @cclist\n";
                print TMP "From: $from_name <$from_email>\n";
                print TMP "Subject: $subject\n";
                print TMP "\r\n";
                print TMP "This is the body of the email.\n";
                print TMP "It was sent on:  $datetime\n";
                print TMP "\r\n";

                close TMP;
}

I made the line I am confused about bold. Not sure if anyone has suggestions, as I am new to Perl too. I also changed the addresses that are used up there, obviously.

Also, if anyone has suggestions, please feel free to jump in here.
 
Old 02-14-2004, 09:13 AM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 58
You could make it a lot simpler by just sending via sendmail:
Code:
#!/usr/bin/perl
$time=localtime(time());
open(MAIL,"|/usr/lib/sendmail -t");
print MAIL "to: recipient\@host.com\n";
print MAIL "from: sender\@host.com\n";
print MAIL "Subject: Updated IP\n\n";
print MAIL "This is the body of the email.\nIt was sent on: $time\n";
close(MAIL);
 
Old 02-14-2004, 09:59 PM   #3
Bladez
LQ Newbie
 
Registered: Aug 2003
Location: Massachusetts
Distribution: Mandriva 2006, Ubuntu 6.06
Posts: 27

Original Poster
Rep: Reputation: 15
Wow.. that was a lot easier than I though it would be. I appreciate the help, a lot! Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl question: how to insert an e-mail address in a script Pastorino Programming 3 08-08-2005 08:28 AM
PostFix & mail routing. abhijeetudas Linux - Networking 0 08-01-2005 01:21 AM
perl script to report new mail nibbler Programming 1 07-02-2005 09:13 AM
howto send a mail with attachment via perl script ? cccc Programming 24 03-05-2004 07:49 PM
I'm looking for a simply perl script to send mail, when ping down cccc Programming 2 01-12-2004 04:36 AM


All times are GMT -5. The time now is 08:55 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration