Hi All,
Long time no see.
I just finished up a new mailing list for my site, and I thought I'd share, as troll boy wanted it, I figured other may as well.
 
Please let me know if this is completely retarted, I couldn't think of a better way to do it, but there may well be one.
 
Warning, it hasn't been very well tested in this form, I'd run it beta for awhile if I was you.
I think I have all the bugs out, but I am not sure.
It's set to go off every 15 minutes from 1am to 5:15, via crontab
It supports normal and HTML versions now.
Sends out in groups of 100 addresses, which you can change, but it's easier on the server this way
Code is better and cleaner and faster now, than the old one
All variables are at the top.
Just set your cron to hit it at the proper times and it should work, oh and make sure you have a header file somewhere for it as well.
If it's acceptable I'll add it to the cvs
Keep in mind, I am still running the old code, so it runs with the old config.php3, I'm not sure how different the new one is.
Just in case you don't have the tables here they are as well.
 
If you find bugs, or have ideas on it let me know.
Troll Boy, don't use that other one I sent you.
 
-Blake
 
#
# Table structure for table 'mailing_list'
#
CREATE TABLE mailing_list (
  id int(30) DEFAULT '0' NOT NULL auto_increment,
  email varchar(100) DEFAULT '' NOT NULL,
  name varchar(100),  
  timestamp timestamp(14),
  html tinyint(2) DEFAULT '0' NOT NULL,
  PRIMARY KEY (id),
  UNIQUE email (email)
);
 
#
# Table structure for table 'mailing_frequency'
#
CREATE TABLE mailing_frequency (
  id int(30) DEFAULT '0' NOT NULL,
  frequency char(30) DEFAULT '' NOT NULL, 
  dayback int(11) DEFAULT '0' NOT NULL
);
 
And now the code.....
..........................
 

#!/usr/local/bin/php
<?php
$rightnow = date("Hi"); //figures out what time it is
$link = mysql_connect("localhost","user","password"); //what db you have the emails in
$thedb = "thenameofthedbwiththeemailinit";
 
//since you want to include a message daily, it's a seperate file
//remember to make it HTML if you are using the HTML option.
//tags are stripped for plain text
 
$filename = "/path/to/a/file/header.txt";
$fd = fopen ($filename, "r");
$header = fread ($fd, filesize ($filename)); 
fclose ($fd);
 
$sitename = "site.com"; //change to your site name, used in the title, and subject, and body of mail
$siteurl = "http://www.site.com"; //again, change to yours, used in same as above
 
if ($html==1)
 {
 $yourtitle = "The  HTML Email"; //gotta change this too, used in HTML title of email
 $mailsubj = " HTML Headlines"; //change this too, used for HTML Subject of HTML mail
 } else  {
  $yourtitle = "The .com Email"; //uh, used not really
  $mailsubj = ".com Headlines"; // Is the subject of plain tect email, change to yours
  }
$sender = "news@site.com"; // this is the who sent this email for both mails
 
//the footer ends up as the last thing in the mail. TAgs are stripped for plain text
//you may want to move this out to a file like the header if you feel the need
$footer = "<P><B>\n\t To add or remove yourself go to:\n<BR>".$siteurl."/mailing_list.php3</B><P></BODY></HTML>";
 
//busy server? set this baby to sleep. 0 means it mails as fast as it can
// it's in seconds
$sleep = 5;
 
//these 2 are used to format the mail to make it shorter and so it wraps at a certain length
$wraplength = 800;
$choplength = 600;
 
///////////////
//so it should work without playing with the code below
//you will most likely need to change the time and the limits below, but it should work
//with out messing with them,may not be what you want though
///////////////
 
if ($rightnow == "0100")
 {
 $limit = "0,100";
 $today = date("w");
 if($today=="1") { $offset = "3"; }
 else { $offset = "2"; }
 $frequency = "DAY";
 $html =0;
 }
elseif ($rightnow == "0115")
 {
 $limit = "99,101";
 $today = date("w");
 if($today=="1") { $offset = "3"; }
 else { $offset = "2"; }
 $frequency = "DAY";
 $html =0;
 }
elseif ($rightnow == "0130")
 {
 $limit = "200,100";
 $today = date("w");
 if($today=="1") { $offset = "3"; }
 else { $offset = "2"; }
 $frequency = "DAY"; 
 $html =0;
 }
elseif ($rightnow == "0145")
 {
 $limit = "300,100";
 $today = date("w");
 if($today=="1") { $offset = "3"; }
 else { $offset = "2"; }
 $frequency = "DAY";
 $html =0;
 
 }
elseif ($rightnow == "0200")
 {
 $limit = "400,100";
 $today = date("w");
 if($today=="1") { $offset = "3"; }
 else { $offset = "2"; }
 $frequency = "DAY";
 $html =0;
 
 }
elseif ($rightnow == "0215")
 {
 $limit = "500,100";
 $today = date("w");
 if($today=="1") { $offset = "3"; }
 else { $offset = "2"; }
 $frequency = "DAY";
 $html =0;
 
 }
elseif ($rightnow == "0230")
 {
 $limit = "600,100";
 $today = date("w");
 if($today=="1") { $offset = "3"; }
 else { $offset = "2"; }
 $frequency = "DAY";
 $html =0;
 
 }
elseif ($rightnow == "0245")
 {
 $limit = "700,100";
 $today = date("w");
 if($today=="1") { $offset = "3"; }
 else { $offset = "2"; }
 $frequency = "DAY";
 $html =0;
 
 }
elseif ($rightnow == "0300")
 {
 $limit = "800,100";
 $today = date("w");
 if($today=="1") { $offset = "3"; }
 else { $offset = "2"; }
 $frequency = "DAY";
 $html =0;
 
 }
elseif ($rightnow == "0315")
 {
 $limit = "900,100";
 $today = date("w");
 if($today=="1") { $offset = "3"; }
 else { $offset = "2"; }
 $frequency = "DAY";
 $html =0;
 
 }
elseif ($rightnow == "0330")
 {
 $limit = "1000,100";
 $today = date("w");
 if($today=="1") { $offset = "3"; }
 else { $offset = "2"; }
 $frequency = "DAY";
 $html =0;
 
 }
elseif ($rightnow == "0345")
 {
 $limit = "1100,100";
 $today = date("w");
 if($today=="1") { $offset = "3"; }
 else { $offset = "2"; }
 $frequency = "DAY";
 $html =0;
 
 }
elseif ($rightnow == "0400")
 {
 $limit = "1200,100";
 $today = date("w");
 if($today=="1") { $offset = "3"; }
 else { $offset = "2"; }
 $frequency = "DAY";
 $html =0;
 
 }
elseif ($rightnow == "0415")
 {
 $limit = "1300,100";
 $today = date("w");
 if($today=="1") { $offset = "3"; }
 else { $offset = "2"; }
 $frequency = "DAY";
 $html =0;
 }
//that was the last of the normal daily emails
 
elseif ($rightnow == "0430")
 {
 $limit = "0,200";
 $today = date("w");
 $html =0;
 if($today=="1")
  {
  $offset = "7";
  $frequency = "MONDAY";
  }
  else { die; }
 }
elseif ($rightnow == "0445")
 {
 $limit = "0,200";
 $today = date("w");
 $html =0;
 if($today=="3")
  {
  $offset = "7";
  $frequency = "WEDNESDAY";
  }
  else { die; }
 }
elseif ($rightnow == "0500")
 {
 $limit = "0,200";
 $today = date("w");
 $html =0;
 if($today=="5")
  {
  $offset = "7";
  $frequency = "FRIDAY";
  }
  else { die; }
 
 }
elseif ($rightnow == "0515")
 {
 $limit = "0,200";
 $today = date("w");
 $html =0;
 if($today=="0")
  {
  $offset = "7";
  $frequency = "SUNDAY";
  }
  else { die; }
 
 }
///here is the experimental HTML one
elseif ($rightnow == "0530")
 {
 $limit = "0,100";
 $today = date("w");
 if($today=="1") { $offset = "3"; }
 else { $offset = "2"; }
 $frequency = "DAY";
 $html = 1;
 
 }
 
 
 
require("./config.php3");
 
function format_mail($str, $char_len)
 {
  GLOBAL $wraplength;
         $str = stripslashes($str);
         $str = strip_tags($str);
         $str = eregi_replace("\n"," ",$str);
         $str = eregi_replace("\t"," ",$str);
         $str = ereg_replace("\r\n"," ",$str);
         $str = eregi_replace("[[:space:]]+", " ", $str);
   $str_out .= substr($str, 0, $wraplength);
   $str = substr($str, 0, $wraplength);
   return $str_out;
 }
function format_html_mail($str, $char_len)
 {
  GLOBAL $wraplength;
         $str = stripslashes($str);
         $str = eregi_replace("\n"," ",$str);
         $str = eregi_replace("\t"," ",$str);
         $str = ereg_replace("\r\n"," ",$str);
         $str = eregi_replace("[[:space:]]+", " ", $str);
   $str_out .= substr($str, 0, $wraplength);
   $str = substr($str, 0, $wraplength);
   return $str_out;
 }
 
function chopit ($str)
 {
GLOBAL $choplength;
$string = substr($str, 0, $choplength);
$pos = strrpos ($string, ".");
$finalstring = substr($string, 0, $pos);
return $finalstring;
 }
 
function build_body($offset)
 {
GLOBAL $offset;
$slash_q = new slashDB;
$bmonth = date("m");
$bday = date("d") - $offset;
$byear = date("Y");
$emonth = date("m");
$eday = date("d");
$eyear = date("Y");
$begdate = date("Y-m-d H:i:s", mktime(0,0,0,$bmonth,$bday,$byear));
$enddate = date("Y-m-d H:i:s", mktime(0,0,0,$emonth,$eday + 1,$eyear));
$sql="SELECT sid, title, dept, aid, date_format(time, \"%W %M %d %h %i %p\") as pretty_time, introtext FROM stories
    WHERE time >= '$begdate' AND time <= '$enddate' ORDER BY time DESC";
 
$slash_q->query($sql);
while($slash_q->next_record())
 {
GLOBAL $siteurl;
        $headline = "\n".$slash_q->Record["title"] . "\n";
        $headline .= "      From the: \"" . $slash_q->Record["dept"] . "\" dept.\n";
        $headline .= "      Posted by: \"" . $slash_q->Record["aid"] . "\" on: " . $slash_q->Record["pretty_time"] . "\n";
        $headline .= "      Story:   " . format_mail($slash_q->Record["introtext"],500) . ".\n";
        $headline .= " ".$siteurl."article.php3?sid=" . $slash_q->Record["sid"]."\n";
        $headline .= "---------------------------------------------------------------------------------------------\n\n";
        $headline = stripslashes($headline);
        $thisHead .= $headline;
 }
 return $thisHead;
 }
 
function build_html_body($offset)
 {
GLOBAL $offset;
$slash_q = new slashDB;
$bmonth = date("m");
$bday = date("d") - $offset;
$byear = date("Y");
$emonth = date("m");
$eday = date("d");
$eyear = date("Y");
$begdate = date("Y-m-d H:i:s", mktime(0,0,0,$bmonth,$bday,$byear));
$enddate = date("Y-m-d H:i:s", mktime(0,0,0,$emonth,$eday + 1,$eyear));
$sql="SELECT sid, title, dept, aid, date_format(time, \"%W %M %d %h %i %p\") as pretty_time, introtext FROM stories
    WHERE time >= '$begdate' AND time <= '$enddate' ORDER BY time DESC";
 
$slash_q->query($sql);
while($slash_q->next_record())
 {
GLOBAL $siteurl;
        $headline = "<B>".$slash_q->Record["title"] . "</B><BR>\n";
        $headline .= "      From the: \"" . $slash_q->Record["dept"] . "\" dept.<BR>\n";
        $headline .= "      Posted by: \"" . $slash_q->Record["aid"] . "\" on: " . $slash_q->Record["pretty_time"] . "<BR>\n";
        $headline .= "      Story:   " . format_mail(chopit($slash_q->Record["introtext"]),500) . ".</I><BR>\n";
        $headline .= "<A HREF=\"".$siteurl."article.php3?sid=" . $slash_q->Record["sid"] . "\">Full Story</A>\n";
        $headline .= "<BR>---------------------------------------------------------------------------------------------<BR>\n\n";
        $headline = stripslashes($headline);
        $thisHead .= $headline;
 }
 return $thisHead;
 }
 
//// mail goes out below here now
 
  $db_results = mysql("$thedb","SELECT email, dayback, frequency
        FROM mailing_list, mailing_frequency
        WHERE mailing_list.id = mailing_frequency.id
        AND mailing_frequency.frequency = '$frequency' 
 AND mailing_list.html = $html
        ORDER BY dayback, mailing_frequency.frequency
 LIMIT $limit;",$link);
 
   ignore_user_abort();
   flush();
 
   $tmpDay = '';
   while($row = mysql_fetch_row($db_results))
 {
GLOBAL $sender;
GLOBAL $html;
///first part is the plain text email
if($html==0)
 {
   set_time_limit(0);
   $header= strip_tags($header);
   $body = $header;
   $body .= build_body($row[2]);
   $body .= strip_tags($footer);
   $subject = $mailsubj;
   $headers = "Reply-To:$sender\r\n";
   $headers .= "From:$sender\r\n";
if (mail($row[0], $subject, $body, $headers))  
         echo "\n" . $row[0]. $row[2] . "\n... sent\n";
      else
  $errors[] = $row[0];
   $tmpDay = $row[2];
   sleep($sleep);
 } //closes if html=0 so that means the HTML goes out from below here
 else
 {
   set_time_limit(0);
   $body = "<html><head><title>" . $yourtitle."</title><BASE HREF=\"".$siteurl."\"></head><body>\r\n\n\n";
   $body .= $header;
   $body .= build_html_body($row[2]);
   $body .= $footer;
   $subject = $mailsubj;
   $headers = "Reply-To:$sender\r\n";
   $headers .= "From:$sender\r\n";
   $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
if (mail($row[0], $subject, $body, $headers))  
         echo "\n" . $row[0]. $row[2] . "\n... sent\n";
      else
  $errors[] = $row[0];
   $tmpDay = $row[2];
   sleep($sleep);
 
 }//closes else for html = 0
    }//closes the mail function
?>