A Working Newsletter


I can offer you a solution that's not super pretty, but it does integrate with PHP-Nuke. It's a replacement for the existing admin/modules/newsletter.php; this works only with versions of PHP-Nuke prior to v7.5, since changes to the structure in 7.5 make this unworkable (maybe by then PHP-Nuke will have something better built in). I'm using this on my v7.2 site with no problems.

You need to make some changes to your PHP-Nuke database first, which means getting access to the database. Hopefully, your hosting provider (if you're using one) can show you how to do that, often through a Web-based interface.

You need to run the following SQL statements in your SQL management interface to create two new tables:

 CREATE TABLE 'nuke_sanews_config' (   'NewsText' text NOT NULL,   'NewsHTML' text NOT NULL,   'NewsFrom' varchar(255) NOT NULL default '',   'NewsType' varchar(4) NOT NULL default 'text',   'NewsSubject' varchar(255) NOT NULL default '' ) TYPE=MyISAM; CREATE TABLE 'nuke_sanews_send' (   'SendID' int(11) NOT NULL auto_increment,   'SendEmail' varchar(255) NOT NULL default '',   'SendSuccess' tinyint(4) NOT NULL default '0',   PRIMARY KEY  ('SendID') ) TYPE=MyISAM AUTO_INCREMENT=1722 ; 

This syntax is valid for MySQL databases but also should work for most other databases with perhaps some minor tweaking; get a SQL guru to help you, if need be.

Next is the entire text of a replacement admin/modules/newsletter.php file; be sure to back up the old file before putting this in its place. Also note a couple of caveats: First, this file isn't localized and doesn't use PHP-Nuke's language files. It's hard-coded in English, so if you want to translate it, that's up to you (and you're welcome to do so). Second, if you want to download this rather than typing it, you can do so from www.scriptinganswers.com/download/newsletter.zip. But I can't provide technical support for the file; you're on your own there, just as you are with PHP-Nuke itself.

So here's the script, which I interrupt briefly to explain what's going on. Don't worry about the line breaks you see; you can type this in exactly as is (please download itI hate to think of anyone typing all this), and it'll work.

[View full width]

<?php /************************************************************************/ /* PHP-NUKE: Web Portal System */ /* =========================== */ /* */ /* Copyright (c) 2002 by Francisco Burzi */ /* http://phpnuke.org */ /* */ /* 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. */ /************************************************************************/ /* */ /* Modified to send bulk email and to have add'l sending options */ /* by Don Jones www.scriptinganswers.com. Not localized (English only). */ /* */ /* You're welcome to modify this or localize it. I don't provide any */ /* support whatsoever for this script. Have fun with it. */ /* */ /************************************************************************/ if (!eregi("admin.php", $_SERVER['PHP_SELF'])) { die ("Access Denied"); } $aid = trim($aid); $result = $db->sql_query("select radminnewsletter, radminsuper from ".$prefix."_authors where aid='$aid'"); list($radminnewsletter, $radminsuper) = $db->sql_fetchrow($result); if (($radminnewsletter==1) OR ($radminsuper==1)) { include("admin/modules/functions_mail.php"); /*********************************************************/ /* Sections Manager Functions */ /*********************************************************/

Following is the code that draws the initial page, which is shown in Figure 49.2. You can type the e-mail address that the newsletter is sent from, as well as a subject. You can enter the number of new news stories (articles), downloads, and so forth; these will be linked in the newsletter to draw users' attention to them.

Figure 49.2. Sending an e-newsletter: Don's way.


Then you provide both a text and HTML-formatted version of your newsletter. In both versions, put the phrase %NewArticles% someplace; it will be replaced with the list of new news stories. Similarly, %NewDownloads% is a placeholder for the list of downloads, and so forth. You can then decide to send a text e-mail, an HTML-only e-mail (not recommended, as it will often be tagged as spam), or both (recommended and the default). You can test your newsletter by sending it just to one address, or you can select from sending it to all subscribed users or all users on your site. Then just click Send.

[View full width]

function newsletter() { global $prefix, $user_prefix, $db, $adminmail, $sitename; include("header.php"); GraphicAdmin(); $srow = $db->sql_numrows($db->sql_query("select * from ".$user_prefix."_users where newsletter='1'")); $urow = $db->sql_numrows($db->sql_query("select * from ".$user_prefix."_users")); $urow--; OpenTable(); echo "<center><font class=\"title\"><b>"._NEWSLETTER."</b></font></center>"; CloseTable(); echo "<br>"; OpenTable(); echo "<center><font class=\"content\"><b>"._NEWSLETTER."</b></font></center>" ."<br><br>" ."<form method=\"post\" action=\"admin.php\">" ."<b>From:</b> <input type=\"text\" name=\"NewsFrom\" size=\"40\" value=\"$adminmail\">" ."<br><br>" ."<b>Subject:</b> <input type=\"text\" name=\"NewsSubject\" size=\"40\" value=\"Our Newsletter\">" ."<br><br>" ."<b>Include links to the following:</b><br>" ."New Articles: <input type=\"text\" name=\"NewArticles\" size=\"4\" value=\"5\"> (replaces %NewArticles%)<br>" ."New Downloads: <input type=\"text\" name=\"NewDownloads\" size=\"4\" value=\"5\"> (replaces %NewDownloads%)<br>" ."New Weblinks: <input type=\"text\" name=\"NewLinks\" size=\"4\" value=\"5\"> (replaces %NewLinks%)<br>" ."New Sections: <input type=\"text\" name=\"NewSamples\" size=\"4\" value=\"5\"> (replaces %NewSamples%)" ."<br><br>" ."<b>Text version:</b><br><textarea name=\"NewsText\" cols=\"50\" rows=\"10\"></textarea>" ."<br><br>" ."<b>HTML version:</b><br><textarea name=\"NewsHTML\" cols=\"50\" rows=\"10 \">&lt;HTML&gt;&lt;BODY&gt;&lt;/BODY&gt;&lt;/HTML&gt;</textarea>" ."<br><br>" ."<b>Send what?</b><br>" ."<input type=\"radio\" name=\"format\" value=\"text\"> Text only<br>" ."<input type=\"radio\" name=\"format\" value=\"html\"> HTML only (not recommended)<br>" ."<input type=\"radio\" name=\"format\" value=\"both\" checked> Both" ."<br><br>" ."<b>Send to whom?</b><br>" ."<input type=\"radio\" name=\"type\" value=\"test\" checked> Just this address: <input type=\"text\" name=\"TestAddress\" size=\"40\"><br>" ."<input type=\"radio\" name=\"type\" value=\"subscribed\"> "._ANEWSLETTER." ($srow "._SUBSCRIBEDUSERS.")<br>" ."<input type=\"radio\" name=\"type\" value=\"all\"> "._MASSMAIL." ($urow "._USERS.")" ."<br><br>" ."<input type=\"hidden\" name=\"op\" value=\"newsletter_send\"><br>" ."<input type=\"submit\" value=\"Send\">" ."</form>"; CloseTable(); include("footer.php"); }

Following is the code that sets up the newsletter to be sent. This pulls the list of downloads, links, and news stories, and constructs the content of your e-mail newsletter. It also pulls the e-mail addresses that the newsletter will go to and puts them into one of the two database tables you created earlier.

[View full width]

function newsletter_send($TestAddress, $NewsFrom, $NewsSubject, $NewArticles, $NewDownloads, $NewLinks, $NewSamples, $NewsText, $NewsHTML, $format, $type) { global $user_prefix, $prefix, $sitename, $db, $nukeurl, $adminmail; $subject = stripslashes($NewsSubject).""; $textcontent = stripslashes($NewsText).""; $htmlcontent = stripslashes($NewsHTML).""; /* Get new downloads */ $dl = $db->sql_query("SELECT lid,title FROM ".$prefix."_downloads_downloads ORDER BY lid DESC LIMIT 0,".$NewDownloads); $dltext = ""; $dlhtml = "<ul>"; while(list($lid,$title) = $db->sql_fetchrow($dl)) { $dltext .= "* ".$title.chr(13).chr(10); $dlhtml .="<li><a href=\"http://www.scriptinganswers.com/cms/modules.php?name=Downloads&d_ op=viewdownloaddetails&l\">".$title."</a></li>"; } $dlhtml .="</ul>"; /* Get new links */ $wl = $db->sql_query("SELECT cid,title FROM ".$prefix."_links_links ORDER BY lid DESC LIMIT 0,".$NewLinks); $wltext = ""; $wlhtml = "<ul>"; while(list($lid,$title) = $db->sql_fetchrow($wl)) { $wltext .= "* ".$title.chr(13).chr(10); $wlhtml .="<li><a href=\"http://www.scriptinganswers.com/cms/modules .php?name=Web_Links&l_op=viewlink&c\">".$title."</a></li>"; } $wlhtml .="</ul>"; /* Get new articles */ $na = $db->sql_query("SELECT sid,title FROM ".$prefix."_stories ORDER BY sid DESC LIMIT 0 ,".$NewArticles); $natext = ""; $nahtml = "<ul>"; while(list($lid,$title) = $db->sql_fetchrow($na)) { $natext .= "* ".$title.chr(13).chr(10); $nahtml .="<li><a href=\"http://www.scriptinganswers.com/cms/modules .php?name=News&file=article&s\">".$title."</a></li>"; } $nahtml .="</ul>"; /* Get new sections */ $sa = $db->sql_query("select artid,title FROM ".$prefix."_seccont ORDER BY sid DESC LIMIT 0,".$NewSamples); $satext = ""; $sahtml = "<ul>"; while(list($lid,$title) = $db->sql_fetchrow($sa)) { $satext .= "* ".$title.chr(13).chr(10); $sahtml .="<li><a href=\"http://www.scriptinganswers.com/cms/modules.php?name=Sections&op= viewarticle&art\">".$title."</a></li>"; } $sahtml .="</ul>"; /* Paste downloads, links, articles into text and HTML */ $textcontent = str_replace("%NewArticles%",$natext,$textcontent); $htmlcontent = str_replace("%NewArticles%",$nahtml,$htmlcontent); $textcontent = str_replace("%NewLinks%",$wltext,$textcontent); $htmlcontent = str_replace("%NewLinks%",$wlhtml,$htmlcontent); $textcontent = str_replace("%NewDownloads%",$dltext,$textcontent); $htmlcontent = str_replace("%NewDownloads%",$dlhtml,$htmlcontent); $htmlcontent = addslashes($htmlcontent); $textcontent = addslashes($textcontent); /* what format are we sending? */ switch($format){ case "text": $formattype="text"; $htmlcontent=""; break; case "html": $formattype="html"; $textcontent=""; case "both": $formattype="both"; } /* save configuration to database */ $q = $db->sql_query("DELETE FROM ".$prefix."_sanews_config"); $sql = "INSERT INTO ".$prefix."_sanews_config (NewsType,NewsFrom,NewsSubject,NewsText ,NewsHTML) VALUES" ."(\"".$formattype."\",\"".$NewsFrom."\",\"".$NewsSubject."\",\"". $textcontent."\",\"".$htmlcontent."\")"; $q = $db->sql_query($sql); /* clear send-to table */ $q = $db->sql_query("DELETE FROM ".$prefix."_sanews_send"); /* populate send-to table */ if($type=="test"){ $sql = "INSERT INTO ".$prefix."_sanews_send (SendEmail) VALUES('".$TestAddress."')"; } else { $sql = "INSERT INTO ".$prefix."_sanews_send (SendEmail) select user_email FROM " .$user_prefix."_users"; if($type=="subscribed"){ $sql .= " WHERE newsletter = 1"; } } $q = $db->sql_query($sql); /* begin sending */ Header("Location: admin.php?op=massmail_send"); }

Here's the function that displays when the newsletter has been properly sent:

 function newsletter_sent() {     include("header.php");     GraphicAdmin();     OpenTable();     echo "<center><font class=\"title\"><b>"._NEWSLETTER."</b></font></center>";     CloseTable();     echo "<br>";     OpenTable();     echo "<center><font class=\"content\"><b>"._NEWSLETTER."</b></font><br><br>";     echo "<b>"._NEWSLETTERSENT."</b></center>";     CloseTable();     include("footer.php"); } 

This is the function that actually sends a newsletter. It uses the same type of loop that the old PHP-Nuke newsletter module did, but it sends to only ten addresses at a time. Each address is marked as successful in the database; after sending to ten addresses, the page asks the Web server to reload the page. This is how the page bypasses the 30-second limit: The page continually reloads itself, and each reload gets its own 30-second limit. Using the page, your browser will just seem to run and run and run, but that's okay. If any errors occur, just hit Refresh in your browser; the script won't resend the newsletter to any addresses that have already been successfully processed.

[View full width]

function massmail_send() { global $user_prefix, $prefix, $sitename, $db, $nukeurl, $adminmail; /* output headers which will cause a reload upon completion */ header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-cache, must-revalidate"); header("Pragma: no-cache"); echo "<center>"; /* retrieve names that need to be sent */ $tosend = $db->sql_query("SELECT SendID FROM ".$prefix."_sanews_send WHERE SendSuccess = 0"); echo "<br><br>Remaining: ".$db->sql_numrows($tosend)."<br><br>(page will redirect in a moment)</center>"; /* finished? */ if($db->sql_numrows($tosend) == 0) { echo "<script language=\"javascript\">\n"; echo "<!--\n"; echo "window.location.href = \"admin.php?op=newsletter_sent\"\n"; echo "// -->\n"; echo "</script>\n"; die(); } /* retrieve configuration */ $config = $db->sql_query("SELECT NewsType,NewsFrom,NewsSubject,NewsText,NewsHTML FROM " .$prefix."_sanews_config"); $row=$db->sql_fetchrow($config); /* string prep */ $newstype = $row['NewsType']; $newsfrom = stripslashes($row['NewsFrom']); $newssubject = stripslashes($row['NewsSubject']); $newshtml = stripslashes($row['NewsHTML']); $newstext = str_replace(chr(13).chr(10),"\n",$row['NewsText']); /* retrieve number of names in batch */ $batchsize = 10; $tosend = $db->sql_query("SELECT SendID, SendEmail FROM ".$prefix."_sanews_send WHERE SendSuccess = 0 LIMIT 0,".$batchsize); /* go through names in batch */ while(list($uid,$email) = $db->sql_fetchrow($tosend)) { $msg = new Email($email, $newsfrom, $newssubject); switch($newstype) { case "text": /* build headers and content - TEXT ONLY */ $msg->TextOnly = true; $msg->Content = $newstext; break; case "html": /* build headers and content - HTML ONLY */ $msg->TextOnly = false; $msg->Content = $newshtml; break; case "both": /* build headers and content - TEXT and HTML */ $msg->SetMultipartAlternative($newstext, $newshtml); break; } /* send */ #echo "<br>".$newstext."<br><br><pre>".$newshtml."</pre>"; $result = $msg->SendMail(); echo "<br>".$email.":".$result; /* mark as successful */ $q = $db->sql_query("UPDATE ".$prefix."_sanews_send SET SendSuccess = 1 WHERE SendID = " .$uid); } /* reload page */ echo "<script language=\"javascript\">\nv; echo "<!--\n"; echo "window.location.href = \"admin.php?op=massmail_send\"\n"; echo "// -->\n"; echo "</script>\n"; #echo "<br><br><a href=\"http://www.scriptinganswers.com/cms/admin .php?op=massmail_send\">MANUAL ADVANCE</a>"; }

Finally, the page winds up with the standard PHP-Nuke structure that coordinates all of the previous functions:

[View full width]

switch ($op) { case "newsletter": newsletter(); break; case "newsletter_send": newsletter_send($TestAddress, $NewsFrom, $NewsSubject, $NewArticles, $NewDownloads, $NewLinks, $NewsText, $NewsHTML, $format, $type); break; case "massmail_send": massmail_send(); break; case "newsletter_sent": newsletter_sent(); break; } } else { echo "Access Denied"; } ?>

And that's it. Now you can send an e-mail newsletter to almost any number of users, right within PHP-Nuke.

Sidebar . FAQ

Help! The newsletter script here doesn't work.

It was written for PHP-Nuke 7.2, using an unmodified installation of PHP-Nuke. Other versions may not work correctly. Double-check your version number.

Also understand that using PHP-Nuke to send mass e-mails isn't the best use of the software. If you have a large mailing list, consider using a free or commercial service like Topica (www.topica.com) to manage your mailing list. They're better set up to handle bigger lists, although they don't integrate directly with PHP-Nuke yet.




    PHP-Nuke Garage
    PHP-Nuke Garage
    ISBN: 0131855166
    EAN: 2147483647
    Year: 2006
    Pages: 235
    Authors: Don Jones

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net