Gossamer Forum
Home : Products : DBMan : Customization :

Auto Notify

Quote Reply
Auto Notify
Hi Carol

I'm using your auto_delete code which works fine...
I would like to notify users via email 2 days prior to record deletion date that they can renew their posting
if they wish.
as I don't totally understand the auto_delete code, I do realiize that most of it could be used to achieve
my objective....I 'm not sure how to pick up the email addresses of postings about to be deleted...could you assist me here...I can include the mail program ok...thanks Rob

sub auto_notify {
$days = 32;
$time = time() - ($days * 86400);
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time);
my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;
$in{'Date-lt'} = "$day-$months[$mon]-$year";
open(FILE, "< $db_file_name") or &cgierr("can't open $db_file_name: $!");
$count++ while <FILE>;
close FILE;
$in{'mh'} = $count;
my ($status,@hits) = &query("mod");
for (0 .. $db_total_hits - 1) {
%tmp = &array_to_hash($_, @hits);
$in{$tmp{$db_key}} = "delete";
}
&delete_records;
}



Quote Reply
Re: Auto Notify In reply to
In Reply To:
I'm using your auto_delete code which works fine...
It's not my code. It is adapted from my code, but I didn't write anything that does anything automatically.

(Just wanted to clear that up. Smile)

What I would do is delete the line

$in{$tmp{$db_key}} = "delete";

and replace it with the mail routine, using

print MAIL "To: $tmp{the name of your email field}\n";

and then delete the line

&delete_records;


JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Auto Notify In reply to
Hi Carol...your right of course....it's not automatic.....thank you for the assistance...it works fine...Rob

Quote Reply
Re: Auto Notify In reply to
Hi, Rob

I like your mod. Could you post a complete one with instruction of how to install it?

Thanks

Long

Quote Reply
Re: Auto Notify In reply to
Hi Long...
it's not my mod, I just came across the orginal code at
http://www.gossamer-threads.com/perl/forum/showflat.pl?Cat=&Board=DBInst&Number=54635&page=&view=&sb=&vc=1

Carol was more than helpfull in assisting me in converting it from a - "delete mod" to a - "notify mod"...you can get the orginal code and instructions on how to install from the above link....and the additional changes required from this page...thanks Rob



Quote Reply
Re: Auto Notify In reply to
I have this work on my site, when a record from a user will be autodeleted in 3 days, or a record from a company in 10 days, he will have a message in his emailbox, including a link, that he only have to click, and the record will stay for a next periode, that is been giving in by adding.

I will post the sub of my cron, that runs every night and also my 'test.cgi', including all the sub's, that you need, whitch is called by clicking the link from out the emailmessage.

If you modify this script and set the variables to the values of your own fields, install it and give it the proper rights (755).

You will have to add a field to your database, whitch contains the value, filled by the user, when he adds a record, if he wants to receive a message ('Ja') or not ('Nee'), with default value ('Ja') defined with radiobuttons in your .cfg.

I have to apologize, this is written in 'Dutch' language.

For any questions, reply on this thread.
Good luck!!
=========================
# Test.cgi
#!/usr/local/bin/perl

require 5.003;


$db_script_path = "/opt/guide/your.server.com/cgi-bin/dbman";
$admin_email = "sales\@autorandstad.nl";
$db_key_pos = 0;
$auth_log_file = $db_script_path . "/default.log";
$auth_pw_file = $db_script_path . "/default.pass";
$db_file_name1 = $db_script_path . "/default.db";
$mailprog = "|/usr/sbin/sendmail -t -oeq";
$db_use_flock = 1;
$db_delim = '|';
$datumnu = &get_date;

local(%in) = &parse_form;


&upd_record;
&html_message;
exit;

sub upd_record {
# --------------------------------------------------------

my ($line, @lines, @data, $output, $found);

open (DB, "<$db_file_name1") or die;
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>; # Slurp the database into @lines..
close DB;

$found = 0; # Make sure the record is in here!
LINE: foreach $line (@lines) {
if ($line =~ /^$/) { next LINE; } # Skip and Remove blank lines
if ($line =~ /^#/) { $output .= $line; next LINE; } # Comment Line
chomp ($line);
@data = &split_decode($line);

if ((&date_to_unix($data[3]) + (86400 * 4)) <= (time)) { $again = 1; } else {$again = 0;}

if (($data[0] eq $in{'ID'}) && ($data[18] eq $in{'userid'}) && ($data[17] eq $in{'email'}) && ($again eq 1)) {
$data[3] = &get_date;
$data[26] = "Ja";
$enddate = &get_date(time() + ($data[19] * 86400));
$output .= join ("|",@data) . "\n";
$found = 1;

$title = "Advertentie in Autorandstad verlengd";
$message = "<center><h2>Advertentie in Autorandstad verlengd</h2>

<b>De nieuwe vervaldatum van uw $data[2] $data[4] is ingesteld op $enddate.</b>


U krijgt tegen die tijd weer een emailbericht.


Succes met de verkoop!!.
</center>";

}
else {
$output .= $line . "\n"; # else print regular line.
}
}
if ($found) {
open (DB, ">$db_file_name1") or die;
if ($db_use_flock) {
flock(DB, 2) or die;}
print DB $output;
close DB; # automatically removes file lock

&auth_logging("record verlengd: $in{'ID'}");
}
else {

$title = "Advertentie in Autorandstad niet verlengd";
$message = "<center><h2>Auto-advertentie niet verlengd,</h2>
<b>u heeft hem misschien al eerder ge-update</b>
<font size=-4>(dit kan slechts 1 keer),</font>

<b>of hij is al verwijderd.</b>

U kunt altijd weer opnieuw een auto invoeren.
 
</center>";
}
}


sub html_message {
# --------------------------------------------------------


print "Content-type: text/html\n\n";

print qq|<HTML><HEAD><TITLE>$title</TITLE></HEAD>
<BODY BACKGROUND="http://www.autorandstad.nl/bg1024dl.JPG" leftMargin=4 topMargin=0 link="#003399" vlink="#003399" alink="red">
<TABLE BORDER=0 WIDTH="755" HEIGHT="100%" VALIGN="TOP">
<TR>
<TD ALIGN="left" VALIGN=TOP WIDTH="120">
$advLinks
</td><td VALIGN="TOP">
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=2 WIDTH="450" ALIGN="CENTER" VALIGN="TOP">
<TR>
<BR><BR><BR>
<TD COLSPAN="2" BGCOLOR="#2E8B57">
<FONT FACE="MS Sans Serif, arial,helvetica" size=1 COLOR="#FFFFFF">
$message
<TD WIDTH="100" VALIGN="TOP" ALIGN=CENTER>
$advRechts</TD></TABLE>
</TR>
</TABLE>
</BODY>
</form>
</HTML>
|;
}

sub get_date {
# --------------------------------------------------------
# Returns the date in the format "dd-mmm-yy".
# Warning: If you change the default format, you must also modify the &date_to_unix
# subroutine below which converts your date format into a unix time in seconds for sorting
# purposes.

my ($time1) = @_[0];
($time1) or ($time1 = time());

my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time1);
my (@months) = qw!Jan Feb Mrt Apr Mei Jun Jul Aug Sep Okt Nov Dec!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;

return "$day-$months[$mon]-$year";
}

sub get_date2 {
# --------------------------------------------------------
# Returns the date in the format "dd-mmm-yy".
# Warning: If you change the default format, you must also modify the &date_to_unix
# subroutine below which converts your date format into a unix time in seconds for sorting
# purposes.

my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime(time());
my (@months) = qw!Jan Feb Mrt Apr Mei Jun Jul Aug Sep Okt Nov Dec!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;

return "$day-$months[$mon]-$year";
}

sub date_to_unix {
# --------------------------------------------------------
# This routine must take your date format and return the time a la UNIX time().
# Some things to be careful about..
# int your values just in case to remove spaces, etc.
# catch the fatal error timelocal will generate if you have a bad date..
# don't forget that the month is indexed from 0!
#
my ($date) = $_[0];
my (%months) = ("Jan" => 0, "Feb" => 1, "Mrt" => 2, "Apr" => 3, "Mei" => 4, "Jun" => 5,
"Jul" => 6, "Aug" => 7, "Sep" => 8, "Okt" => 9, "Nov" => 10,"Dec" => 11);
my ($time);
my ($day, $mon, $year) = split(/-/, $_[0]);
unless ($day and $mon and $year) { return undef; }
unless (defined($months{$mon})) { return undef; }

use Time::Local;
eval {
$day = int($day); $year = int($year) - 1900;
$time = timelocal(0,0,0,$day, $months{$mon}, $year);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}

sub auth_logging {
# --------------------------------------------------------
# Logs an action to the database. Takes as input an action, and
# optionally a user id. If no user id is passed in, it get's one from
# the global $db_userid.

my ($action, $uid) = @_;
my ($time) = &get_time; # Change time/date format globally
my ($date) = &get_date2; # in get_time and get_date.

open (LOG, ">>$auth_log_file") || &cgierr("unable to open log file: $auth_log_file. Reason: $!\n");
flock (LOG, 2) unless (!$db_use_flock);
print LOG "$uid $action at $time on $date from $ENV{'REMOTE_HOST'}\n";
close LOG; # releases file lock.
}

sub get_time {
# --------------------------------------------------------
# Returns the time in the format "hh-mm-ss".
#
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime(time());
($sec < 10) and ($sec = "0$sec");
($min < 10) and ($min = "0$min");
($hour < 10) and ($hour = "0$hour");

return "$hour:$min:$sec";
}

sub split_decode {
# --------------------------------------------------------
# Takes one line of the database as input and returns an
# array of all the values. It replaces special mark up that
# join_encode makes such as replacing the '``' symbol with a
# newline and the '~~' symbol with a database delimeter.

my ($input) = shift;
$input =~ s/\Q$db_delim\E$/$db_delim /o; # Add a space if we have delimiter new line.
my (@array) = split (/\Q$db_delim\E/o, $input);
for ($i = 0; $i <= $#array; $i++) {
$array[$i] =~ s/~~/$db_delim/og; # Retrieve Delimiter..
}
return @array;
}

sub parse_form {
# --------------------------------------------------------
# Parses the form input and returns a hash with all the name
# value pairs. Removes SSI and any field with "---" as a value
# (as this denotes an empty SELECT field.

my (@pairs, %in);
my ($buffer, $pair, $name, $value);

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}
PAIR: foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);

$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/\"//g;

$value =~ s///g; # Remove SSI.
if ($value eq "---") { next PAIR; } # This is used as a default choice for select lists and is ignored.
(exists $in{$name}) ?
($in{$name} .= "~~$value") : # If we have multiple select, then we tack on
($in{$name} = $value); # using the ~~ as a seperator.
}
return %in;
}

=============================
=============================
# The part of my 'cron'
=============================

sub upd_record {
# --------------------------------------------------------

my (@data, $output);

open (DB, "$db_file_name1") or die;
if ($db_use_flock) { flock (DB, 1); }
@lines = <DB>;
close DB;
LINE: foreach $line (@lines) {
chomp ($line);
@data = &split_decode2 ($line);

if ($data[1] eq "P") {$days_left = 3;}
else {$days_left = 10;}

$days_ago = $data[19]-$days_left;

if (($data[26] eq "Nee") or ($data[26] eq "verzonden")) {
$output .= $line . "\n";
next LINE;
}
# Als de auto over 3 of 10 dagen verwijderd wordt
elsif ((&date_to_unix($data[3]) + (86400 * $days_ago)) <= (time)) {

$exp_date = &get_date2($date_un + (86400 * $days_left));

# send an email to warn them
open (MAIL, "$mailprog") or die;
print MAIL "To: $data[17]($data[22])\n";
print MAIL "From: $admin_email(Autorandstad Sales)\n";
print MAIL "Subject: $db_name vervallen auto-advertentie\n\n";
print MAIL "-" x 60 . "\n\n";
print MAIL "Geachte $data[22],\n\n";
print MAIL "U heeft $days_ago dagen terug een auto opgegeven bij Autorandstad.nl.\n";
print MAIL "Het gaat om de $data[2] $data[4] van $data[6], vraagprijs f. $data[15].\n";
print MAIL "Deze wordt over $days_left dagen, op $exp_date, automatisch verwijderd.\n";
print MAIL "Als de auto inmiddels verkocht is, hoeft u niets te doen.\n";
print MAIL "Als u de advertentie nog een periode wilt laten staan, klik dan op onderstaande link.\n\n";
print MAIL "http://www.autorandstad.nl/cgi-bin/dbman/test.cgi?ID=$data[0]&userid=$data[18]&email=$data[17]\n\n";
print MAIL "Uw auto is tot nu toe $data[25] keer bekeken.\n";
print MAIL "Alvast bedankt voor uw aandacht en succes!!\n\n";
print MAIL "Met vriendelijke groet,\n";
print MAIL "Autorandstad Sales.";
close (MAIL);

# save the date the warning was sent
$data[26] = "verzonden";
&auth_logging('waarschuwing verzonden, bijna autodelete', $data[0]);
# add the new information back into the database
$output .= join ("|",@data) . "\n";
}
else { $output .= $line . "\n"; }
}
# write the information back to the database
open (DB, ">$db_file_name1") or die;
if ($db_use_flock) {flock(DB, 2) or die;}
print DB "$output";
close DB;
}




----------
Mart.