/***************************************************************************
PHP mailing list script v1.0
(c) Kai Blankenhorn
www.bitfolge.de/en
kaib@bitfolge.de
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
***************************************************************************/
?>
Mailing list cron job
include("pop3.php");
// you may change these to suit you best
$dbServer = "localhost";
$dbUser = "";
$dbPassword = "";
$dbName = "";
function sendWelcomeMessage($address, $listPrefix, $listDescription, $listAddress, $adminAddress) {
$body = "Welcome to the $listPrefix list!\r\n\r\n";
$body.= "List description:\r\n";
$body.= "$listDescription\r\n\r\n";
$body.= "To send messages to the list, send a message to $listAddress. All emails sent to this address will be forwarded to all list subscribers.\r\n\r\n";
$body.= "To unsubscribe from the list, send a message with subject 'UNSUBSCRIBE' (without quotes) to $listAddress.\r\n";
$body.= "mailto:$listAddress?subject=UNSUBSCRIBE\r\n";
mail($address, "Your subscription to $listPrefix", wordwrap($body), "From: $listPrefix administrator <$adminAddress>");
}
function sendGoodbyeMessage($address, $listPrefix, $listDescription, $listAddress, $adminAddress) {
$body = "You have chosen to unsubscribe from the $listPrefix list. Sorry to see you leave.\r\n\r\n";
$body.= "If you decide to enter the list again, send a message with subject 'SUBSCRIBE' (without quotes) to $listAddress.\r\n";
$body.= "mailto:$listAddress?subject=SUBSCRIBE\r\n";
mail($address, "Subscription cancelled: $listPrefix mailing list", wordwrap($body), "From: $listPrefix administrator <$adminAddress>");
}
function sendInvitationMessage($address, $listPrefix, $listDescription, $listAddress, $adminAddress) {
$body = "You have sent an email to a private mailing list. Subscription is required to post messages on this list.\r\n\r\n";
$body.= "List description:\r\n";
$body.= "$listDescription\r\n\r\n";
$body.= "If you would like to enter the list, send a message with subject 'SUBSCRIBE' (without quotes) to $listAddress.\r\n";
$body.= "mailto:$listAddress?subject=SUBSCRIBE\r\n";
mail($address, "Want to join the $listPrefix mailing list?", wordwrap($body), "From: $listPrefix administrator <$adminAddress>");
}
function sendApprovalMessages($address, $listPrefix, $listDescription, $listAddress, $adminAddress) {
$body = "Your admission request for the $listPrefix has been sent to the list administrator ($adminAddress). You will be informed as soon as the administrator has decided over your request.\r\n\r\n";
mail($address, "Subscription request for $listPrefix", wordwrap($body), "From: $listPrefix administrator <$adminAddress>");
$body = "$address has asked for admission to the $listPrefix mailing list.\r\n\r\n";
$body.= "To grant the request, send a message with subject 'APPROVE $address' (without quotes) to $listAddress.\r\n";
$body.= "mailto:$listAddress?subject=APPROVE%20$address\r\n";
mail($adminAddress, "Subscription request for $listPrefix", wordwrap($body), "From: $listPrefix administrator <$adminAddress>");
}
// do NOT change anything from here on (except if you know what you're doing)
mysql_connect($dbServer, $dbUser, $dbPassword);
mysql_select_db($dbName);
$lists = mysql_query("SELECT * FROM lists");
while ($list = mysql_fetch_object($lists)) {
echo "Processing list '$list->prefix'...
";
$user = $list->account;
$password = $list->password;
$apop = 0;
$pop3_connection = new pop3_class;
$pop3_connection->hostname = $list->server;
if(($error = $pop3_connection->Open())=="") {
if(($error = $pop3_connection->Login($user,$password,$apop))=="") {
if(($error = $pop3_connection->Statistics(&$messages,&$size))=="") {
$result = $pop3_connection->ListMessages("",0);
if(getType($result)=="array") {
for(Reset($result); $msgNumberRetrieveMessage(Key($result), &$header, &$body, -1))=="") {
$messageBody = join($body,"\n");
$messageHeader = "";
for ($i=0; $iaddress";
preg_match("/<([^@]*@[^>]*)>/", $messageFrom, $matches);
$senderAllowed = mysql_query("SELECT COUNT(*) as allowed FROM subscribers WHERE address='".$matches[1]."' AND listID=$list->ID AND approved=1");
$sender = mysql_fetch_object($senderAllowed);
$senderIsInList = $sender->allowed;
if (strtoupper($messageSubject == "UNSUBSCRIBE")) {
echo " performing unsubscribe for ".$matches[1];
if ($senderIsInList) {
@mysql_query("DELETE FROM subscribers WHERE address='".$matches[1]."' AND listID=".$list->ID);
echo " - sending goodbye message";
sendGoodbyeMessage($matches[1], $list->prefix, $list->description, $list->address, $list->adminAddress);
} else echo " - cancelled: not in list";
echo "
";
$pop3_connection->DeleteMessage(Key($result));
continue;
}
if (strtoupper($messageSubject == "SUBSCRIBE")) {
echo " performing subscribe for ".$matches[1];
if (!$senderIsInList) {
if ($list->approveMembers == 0) {
$senderApproved = 1;
} else {
$senderApproved = 0;
}
mysql_query("INSERT INTO subscribers SET address='".$matches[1]."', listID=".$list->ID.", approved=$senderApproved");
if ($senderApproved) {
echo " - sending welcome message";
sendWelcomeMessage($matches[1], $list->prefix, $list->description, $list->address, $list->adminAddress);
} else {
echo " - sending approval messages";
sendApprovalMessages($matches[1], $list->prefix, $list->description, $list->address, $list->adminAddress);
}
} else echo " - cancelled: already in list";
echo "
";
$pop3_connection->DeleteMessage(Key($result));
continue;
}
if (strtoupper(substr($messageSubject,0,7)) == "APPROVE") {
if ($matches[1]==$list->adminAddress) {
$requester = substr($messageSubject,8);
echo " performing approval for ".$requester.;
if (!$senderIsInList) {
mysql_query("UPDATE subscribers SET approved=1 WHERE address='$requester'");
echo " - sending welcome message";
sendWelcomeMessage($requester, $list->prefix, $list->description, $list->address, $list->adminAddress);
} else echo " - cancelled: already in list";
echo "
";
$pop3_connection->DeleteMessage(Key($result));
}
continue;
}
$messageSubject = "[".$list->prefix."] ".$messageSubject;
if ($list->membersOnly) {
if (!$senderIsInList) {
echo " skipping '$messageSubject': Sender not in list ($matches[1])";
if ($list->inviteMembers) {
echo " - sending invitation message";
sendInvitationMessage($matches[1], $list->prefix, $list->description, $list->address, $list->adminAddress);
}
echo "
";
$pop3_connection->DeleteMessage(Key($result));
continue; // next message
}
}
if ($list->moderated) {
}
$subscribers = mysql_query("SELECT address FROM subscribers WHERE listID='".$list->ID."' AND approved=1");
echo " sending '$messageSubject'...
";
while ($subscriber = mysql_fetch_object($subscribers)) {
mail($subscriber->address, $messageSubject, $messageBody, $messageHeader);
$pop3_connection->DeleteMessage(Key($result));
}
}
}
} else
$error=$result;
}
}
$pop3_connection->Close();
}
if($error!="")
echo "Error: ",HtmlSpecialChars($error),"
";
}
?>