CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Email notify. Need your help!!

 



thuyduong
Novice

Dec 15, 2002, 11:04 PM

Post #1 of 5 (732 views)
Email notify. Need your help!! Can't Post

Hi all,

I have a flat text database, it have 6 fields,

Name | Email | SFC Number | Date Start | Expire date | Comments

thuyduong| thuyduong@yahoo.com |123456|12/11/2002|12/15/2002|Testing

britney| britney@yahoo.com|987654|12/02/2002|12/20/2002|Hello

thuyduong| thuyduong@yahoo.com |12226|12/10/2002|12/15/2002|wow

thuyduong| thuyduong@yahoo.com |123456|12/09/2002|12/15/2002|Testing

1. How can I write the script so if the SFC will be expire in 3 days or expire it wil mail to Name and tell him, it is expired.

2. If thuyduong has 10 expire items , can I have only one mail send to me include all the SFC number with the expire date ? I really don't want to have 10 mail send to me every day until I chnage the expire date.

Please help me, here is my code and i think this will mail to me every item expire.


Code



#!/usr/dist/share/perl
use Time::Local;
$today = timelocal(localtime);
# Full path to sendmail on your system
$mailprog = "|/usr/lib/sendmail -t";
sub date_to_unix {
my $date = @_[0];
my ($time);
my ($mon, $day, $year) = split(/\//, $_[0]);
my ($hour, $min, $sec) = split(/:/, $_[0]);
unless ($day and $mon and $year) { return undef; }

eval {
$mon = int($mon) - 1; $day = int($day); $year = int($year) - 1900;
# $time = timelocal($hour, $min, $sec, $day, $mon, $year);
$time = timelocal(0,0,0,$day,$mon,$year);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}
open (DATA,"default.db") || die ("Can't Open data File \n");
@data=<DATA>; close DATA;
foreach $line (@data) {
($name, $email , $sfc, $startdate, $expiredate, $comments)=split(/\|/,$line);
my $epdate = &date_to_unix($expiredate);
if ($epdate <= ($today + 259200)) { # within 3 days
{
open (Alertmail, "$mailprog") || &cgierr("Can't start mail program");
print Alertmail "To:$email,you.me\@test.com\n";
print Alertmail "From: you.me\@test.com\n";
print Alertmail "Subject: $sfc is expired . \n\n";
print Alertmail "Hi $name,\n\n";
print Alertmail "$sfc will be expired . Please update the expire date";
print Alertmail "-" x 75 . "\n\n";
print Alertmail "Regards,\n\n";
print Alertmail "Tigger, \n\n";
print Alertmail "\n\n";
}
close (Alertmail); }



davorg
Thaumaturge / Moderator

Dec 16, 2002, 1:26 AM

Post #2 of 5 (727 views)
Re: [thuyduong] Email notify. Need your help!! [In reply to] Can't Post

Instead of sending the email in your foreach loop, build up a data structure containing details of the emails you need to send. Then once you've finished processing the file, you can use the data structure to work out which emails you need to send.

I'd suggest a hash where the keys are the email addresses and the values are references to a arrays containing the expired data.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


thuyduong
Novice

Dec 16, 2002, 12:15 PM

Post #3 of 5 (721 views)
Re: [davorg] Email notify. Need your help!! [In reply to] Can't Post

Hi Dave,

I am new to Perl and just learn by example, can you rewrite the code so I can learn more. I hope you don't mind to share.

Thanks, TD.


davorg
Thaumaturge / Moderator

Dec 17, 2002, 1:18 AM

Post #4 of 5 (714 views)
Re: [thuyduong] Email notify. Need your help!! [In reply to] Can't Post

Well I'm not going to write your code for you. But here's a suggestion of the kind of thing I'd do (untested).

Code
#!/usr/dist/share/perl 

use strict;
use warnings;
use Time::Local;

my %emails;

open (DATA,"default.db") || die ("Can't Open data File: $!\n");
foreach (<DATA>) {
my ($name, $email , $sfc, $startdate, $expiredate,
$comments) = split(/\|/,$line);

if (expiring($expiredate)) {
push @{$emails{$email}}, [$name, $sfc, $expiredate];
}
}
close DATA;

foreach (keys %emails) {
expiry_email($_, @{$emails{$_}});
}

This builds up a hash. The keys of the hash are the email addresses and the values are references to array of SFCs that are about to expire and belong to that email address.

You'll need to write the two functions expiring() and expiry_email(). They'll look a bit like this.

Code
# sub expiring is passed a date in the format DD/MM/YYYY. 
# It returns true if that date is less than three days away.
sub expiring {
my $date = shift;

my ($d, $m, $y) = split m|/|, $date;

$date = timelocal 0, 0, 0, $d, $m - 1, $y - 1900;

return $date - time < (3 * 24 * 60 * 60);
}

# sub expire_email is passed an email address and a list
# of SFCs. It emails the given address saying that the SFCs
# are about to expire
sub expire_email {
my ($email, @sfcs) = @_;

my @list = map { $_->[1] } @sfcs;

open MAIL, '|/usr/lib/sendmail -t'
or die "Can't open sendmail: $!\n";

print MAIL <<"END_OF_MAIL";
to:$email,you.me\@test.com
from: you.me\@test.com
subject: @sfcs are expired.

Hi $sfc[0][0],

The following SFCs will soon expire.
Please update the expiry date.
END_OF_MAIL

for (@sfcs) {
print MAIL "\t$_->[1]\t$_->[2]\n";
}

print MAIL '-' x 75 . "

Regards,

Tigger";

close MAIL;
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


thuyduong
Novice

Dec 19, 2002, 8:31 AM

Post #5 of 5 (703 views)
Re: [davorg] Email notify. Need your help!! [In reply to] Can't Post

Hi Dave,

I modify the code to fit with my case and it works perfectly.

Thank you for your help.

Merry Christmas and Happy New Year.

TD.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives