#!/usr/local/bin/perl
use strict;
use DBI;
use CGI;
use MIME::Lite;
use POSIX qw( strftime );
my $mmddyyyy = strftime("%m/%d/%Y", localtime);
my $mm = strftime("%m", localtime);
my $dd = strftime("%d", localtime);
my $yy = strftime("%Y", localtime);
# Enter Database info
my $USERNAME = "888";
my $PASSWORD = "888";
my $DATABASE = "888";
my $dbh = DBI->connect("dbi:mysql:$DATABASE:localhost:3306", "$USERNAME", "$PASSWORD");
# if ID is an int, this should be "ID = 0" but if it is a char or varchar, it is correct
my $settings1 = $dbh->prepare("select * From settings WHERE ID = '0'");
$settings1->execute();
# since you are selecting * that must include ID, but you don't have ID in this list.
# Assuming it is the first field in the row, I've added it
while (my ($ID, $Path, $Ops_Email, $From_Email, $Subject, $Monthly_Email, $Email_Reminders) = $settings1->fetchrow_array()) {
# move on if we aren't doing anything
next unless $Email_Reminders == 1 or $Monthly_Email == 1;
# Daily Reminders
if ($Email_Reminders == 1) {
# We'll add the start and end of the body only if there were any birthdays today
my ($dailycount,$body);
$sth->execute();
while(my ($Name, $Address, $City, $State, $Postal, $Country, $B_Date) = $sth->fetchrow_array()) {
# Add one to the $dailycount
$dailycount++;
# Get just the year of $B_Date by getting rid of everything after the first dash
(my $B_Year = $B_Date) =~ s/-.*//;
my $age = $yy - $B_Year;
$body .= " $Name (Turns $age!)\n";
}
$sth->finish;
$body .= "\n";
# don't send any if there are none
if ($dailycount > 0) {
$body = <<EOL;
Hello!,
This Is Your Automated Birthday Reminder.
The following people have their birthday TODAY:
$body
This is an automated message, please do not respond.
EOL
# You are sending one email per row in the table "settings" so it must be within that loop
my $msg = MIME::Lite->new(
From =>$From_Email,
To =>$To_Email,
Subject =>$Subject,
Data =>$body
);
### Send in the "best" way (the default is to use "sendmail"):
$msg->send;
}
}
# end Daily Reminders
# Do monthly reminders if it is the first of the month
if ($dd == 1) {
################
# MONTHLYEMAIL #
################
# prepare the query
my $sthMonthly = $dbh->prepare("select * From teamdates WHERE `B_Date` LIKE '%-$mm-%'");
# execute the query
$sthMonthly->execute();
# We'll add the start and end of the body only if there were any birthdays today
my ($monthcount,$body);
while(my ($Name, $B_Date) = $sthMonthly->fetchrow_array()) {
$monthcount++;
# Get just the year of $B_Date by getting rid of everything after the first dash
(my $B_Year = $B_Date) =~ s/-.*//;
my $age = $yy - $B_Year;
$body .= " $Name (Turns $age2!) $B_Date\n";
};
if ($monthcount) {
## Mail Body
my $body = <<EOL;
Hello!,
This Is Your Automated Reminder.
The following people have their birthday this MONTH:\n\n";
$body
This is an automated message, please do not respond.
EOL
my $msg = MIME::Lite->new(
From =>$From_Email,
To =>$Ops_Email,
Subject =>$Subject,
Data =>$body
);
### Send in the "best" way (the default is to use "sendmail"):
$msg->send;
}
}
}
|