I need to use a php file that I can run as a cronjob that would do the following:
- Read my email POP3 account.
- Check if it is bounced, failed, undeliverable.
- Check which email is in the body and delete it from the members list.
I have a cgi script that can do similar but would like to do it in PHP.
I have attaced the cgi file
#!/usr/bin/perl
use CGI qw (:standard);
use Time::Local;
use Net:😛OP3;
$time = time();
$form = new CGI;
$realdate=&getdate;
REQUIRES FULL PATH TO config.pl
require "/home/antaresc/public_html/cgi-bin/config.pl";
DO NOT EDIT
&connect_sql;
&setup;
@bounces = ("undeliver","nondeliverable","fail","error","rejected","returned");
@removes = ("remove","unsubscribe");
%messages = ();
$pop = Net:😛OP3->new($pop3) || &error("Could not connect to $pop3 ($!)");
$pop->login($username, $password) || &error("Could not login to $pop3 ($!)");
$messages = $pop->list;
$subcount = 0;
$unsubcount = 0;
$delcount = 0;
print $form->header;
print qq|<html><head><title>AutoMail Subscriber</title></head><body>|;
foreach $messageid (keys %$messages) {
$mymessage = $pop->get($messageid);
$to = "";
$email = "";
$email2 = "";
$subject = "";
$firstname = "";
$lastname = "";
$listid = "";
$ip = "";
$tid = "";
$bounced = "";
if($mymessage) {
@messages = @$mymessage;
foreach $line (@messages) {
if ($line =~ m/To: /) {
$line =~ s/To: // unless ($line =~ m/Reply-To:/i);
$line =~ s/<//gm;
$line =~ s/>//gm;
chomp($line);
$to = $line;
$listid = $line;
$listid =~ s/@//gm;
$listid =~ s/$domain//gm;
}
if ($line =~ /From:/io) {
$line =~ s/From: //;
$line =~ s/<//gm;
$line =~ s/>//gm;
$line =~ s/\"//gm;
chomp($line);
@field = split(/ /,$line);
foreach $field(@field) {
if($field =~ /@/) { $email = $field; }
$firstname=$field[0];
$lastname=$field[1];
if($firstname =~ /@/){$firstname = "Friend";}
if($lastname =~ /@/){$lastname = "";}
}
}
if($line =~ m/Received: /) {
$line =~ s/Received: from //;
chomp($line);
$ip = $line;
}
if($line =~ m/Subject: /) {
$line =~ s/Subject: //;
chomp($line);
$tid = $line;
$bounced = $line;
}
if($line =~ m/X-Failed-Recipients: /) {
$line =~ s/X-Failed-Recipients: //;
chomp($line);
$bademail = $line;
}
} #foreach line
$to =~ tr/A-Z/a-z/;
$email =~ tr/A-Z/a-z/;
$bademail =~ tr/A-Z/a-z/;
$bounced =~ tr/A-Z/a-z/;
foreach $bounce (@bounces) {
if($bounced =~ m/$bounce/) { &delete_bounce; }
}
foreach $removal (@removes) {
if($bounced =~ m/$removal/) { &unsubscribe; }
}
$SQL = "SELECT ID FROM lists WHERE ID = '$listid'";
&my_sql;
while ($column = $sth->fetchrow_hashref){
$ID = $column->{'ID'};
}
$sth->finish;
if($ID eq $listid) { &subscribe; }
$pop->delete($messageid);
$delcount++;
}#if mymessage
}#foreach messageid
$pop->quit(); #quit and delete messages
print qq|
<p>New subscriptions: $subcount<br>
Removed from database: $unsubcount<br>
Deleted from server: $delcount</p>
</body></html>
|;
exit;
################# SUBSCRIBE ##################################
sub subscribe {
$SQL = "SELECT listid,email FROM recipients WHERE email='$email' AND listid='$listid'";
&my_sql2;
while ($column = $sth->fetchrow_hashref){
$listid2 = $column->{'listid'};
$email2 = $column->{'email'};
}
$sth->finish;
if($listid eq $listid2 && $email eq $email2){
$SQL = "UPDATE recipients SET addtime = '$time', ipaddress = '$ip' WHERE email = '$email' AND listid='$listid'";
&my_sql;
$sth->finish;
}
else{
&insert;
}
$subcount++;
}
############### UNSUBSCRIBE ##############
sub unsubscribe{
$SQL="DELETE FROM recipients WHERE email = '$email'";
&my_sql;
$sth->finish;
}
############### DELETE BOUNCE ##############
sub delete_bounce{
$SQL="DELETE FROM recipients WHERE email = '$bademail'";
&my_sql;
$sth->finish;
}
######################### CHECK EMAIL ########################
sub check_email{
}
################### INSERT RECIPIENT ################
sub insert{
$SQL = "INSERT INTO recipients VALUES ('ID','$listid','$tid','$firstname','$lastname','$email','$time','$ip')";
&my_sql;
$sth->finish;
########## SEND IMMEDIATE AUTORESPONDER ##########
$SQL = "SELECT fromname,fromemail,signature FROM setup";
&my_sql;
while ($column = $sth->fetchrow_hashref){
$fromname = $column->{'fromname'};
$fromemail = $column->{'fromemail'};
$signature = $column->{'signature'};
}
$sth->finish;
$SQL = "SELECT subject,message FROM messages WHERE listid='$listid' AND delay='0'";
&my_sql;
while ($column = $sth->fetchrow_hashref){
$subject = $column->{'subject'};
$message = $column->{'message'};
}
$sth->finish;
$subject =~ s/<firstname>/$firstname/g;
$subject =~ s/<lastname>/$lastname/g;
$subject =~ s/<date>/$realdate/g;
$message =~ s/<firstname>/$firstname/g;
$message =~ s/<lastname>/$lastname/g;
$message =~ s/<email>/$email/g;
$message =~ s/<date>/$realdate/g;
$subject =~ s/\\'/\'/g;
$message =~ s/\\'/\'/g;
open (MAIL, "| $sendmail -t") || die "I can't open sendmail\n";
print MAIL "To: $firstname $lastname <$email>\n";
print MAIL "From: $fromname <$fromemail>\n";
print MAIL "Subject: $subject\n";
print MAIL "Content-Type: text/plain\n";
print MAIL "Content-Transfer-Encoding: 7bit\n";
print MAIL "\n";
print MAIL qq|$message
$signature
To remove yourself from this list, click on this link:
$removeurl?em=$email
<a href="$removeurl?em=$email">
AOL Users Click Here to Unsubscribe</a>|;
close(MAIL);
}
#################### DATE ################################
sub getdate {
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime(time());
my (@months) = qw!January February March April May June July August September October November December!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;
return "$months[$mon] $day, $year";
}
#################### ERROR ################################
sub error {
my $error = shift;
print qq|
Error: $error
|;
exit;
}