Mike Jolley Description: Allows anyone to register for a newsletter/mailing list. Automatically adds registered users to the list. *note: Mail() function headers based on the 'mass mailer' plugin by Brance Gillihan (http://www.clearblueproductions.com). Thanks to Anthony Brewwit (www.designbit.co.uk for the form validation) ======================================================================================== � Copyright 2006 Mike Jolley (jolley dot small at googlemail dot com) 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. */ function wp_maillist($content) { global $submenu; add_management_page('MailList', 'MailList', 6, basename(__FILE__), 'maillist_admin'); add_submenu_page('post.php', __("Write email", "MailList"), __("Write email", "MailList"), 6, basename(__FILE__), 'maillist_write'); } add_action('admin_menu', 'wp_maillist'); function maillist_css() { ?> get_results("SELECT * FROM `WP_MAILLIST` WHERE user_email='$user_email' LIMIT 1;"); if ( empty($compare) ) { # add if not there $num = rand(100000, 999999); $wpdb->query("INSERT INTO WP_MAILLIST (mail_key, user_name, user_email, user_registered, listed) VALUES ('$num', '$user_name', '$user_email', 'false', 'true')"); } else { $wpdb->query("UPDATE WP_MAILLIST SET listed='true' WHERE user_email='$user_email';"); $code='3'; } }} if ($code=='1'){ $forms='

You have successfully joined the mailing list.

'; } elseif ($code=='2'){ $forms='

Please fill in all of the fields.

'; $forms.='
'; $forms.='
'; $forms.='
'; $forms.='
'; $forms.='
'; $forms.='
'; $forms.='
'; } elseif ($code=='3'){ $forms='

You are in the mailing list.

'; } else { $forms='
'; $forms.='
'; $forms.='
'; $forms.='
'; $forms.='
'; $forms.='
'; $forms.='
'; } # string replace, looking for $content = str_replace("", "$forms", "$content"); return $content; } add_action('wp_head', 'maillist_form_css'); function maillist_form_css() { echo ''; } function maketables(){ #check if the tables exist already global $wpdb, $tableposts, $tablepost2cat; $tableExists = false; $tableExists2 = false; $tableExists3 = false; $tables = $wpdb->get_results("show tables;"); foreach ( $tables as $table ) { foreach ( $table as $value ) { if ( $value == WP_MAILLIST ) {$tableExists=true;} if ( $value == WP_MAILLIST_OPTIONS ) {$tableExists2=true;} if ( $value == WP_MAILLIST_CACHE ) {$tableExists3=true;} } } if ( !$tableExists2 ) { $sql = "CREATE TABLE WP_MAILLIST_OPTIONS ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `sender` TEXT NULL , `reply` TEXT NULL , `title` TEXT NULL , `postsnumber` TEXT NULL , `postscats` TEXT NULL , `footer` TEXT NULL , PRIMARY KEY ( `id` ) )"; $wpdb->get_results($sql); $wpdb->query("INSERT INTO WP_MAILLIST_OPTIONS (sender, reply, title, postsnumber, postscats, footer) VALUES ('LorumIpsum.com', 'Lorum@Ipsum.com', 'Recent articles from LorumIpsum.com', '4', '15,13', 'Copyright LorumIpsum.com 2006')"); } if ( !$tableExists3 ) { $sql = "CREATE TABLE WP_MAILLIST_CACHE ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `user_id` INT UNSIGNED NOT NULL , PRIMARY KEY ( `id` ) )"; $wpdb->get_results($sql); } if ( !$tableExists ) { $sql = "CREATE TABLE WP_MAILLIST ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `mail_key` FLOAT UNSIGNED NOT NULL, `user_name` VARCHAR ( 20 ) NOT NULL , `user_email` VARCHAR ( 100 ) NOT NULL UNIQUE, `user_registered` VARCHAR ( 20 ) NULL , `listed` VARCHAR ( 20 ) NOT NULL , PRIMARY KEY ( `id` ) )"; $wpdb->get_results($sql); } #return to where called from return; } function reindex(){ #this function checks whether any new users exist, if so adding them to the mailing list (if they havent already unsubscibed) global $wpdb; $users = $wpdb->get_results("SELECT * FROM `wp_users`;"); foreach ($users as $user) { # check users table against mailing list table $compare = $wpdb->get_results("SELECT * FROM `WP_MAILLIST` WHERE user_email='$user->user_email' LIMIT 1;"); if ( empty($compare) ) { if ( !empty($user->user_email) ) { # add if not there $num = rand(100000, 999999); $wpdb->query("INSERT INTO WP_MAILLIST (mail_key, user_name, user_email, user_registered, listed) VALUES ('$num', '$user->user_nicename', '$user->user_email', 'true', 'true')"); } }//end compare if else { $id=$compare[0]->id; //they are registered and in maillist, so make sure they show up as 'registered; if ($compare[0]->user_registered=='true'){ //do nothing } else { $wpdb->query("UPDATE WP_MAILLIST SET user_registered='true',user_name='$user->user_nicename' WHERE id=$id;"); } } }//end foreach #now check for deleted users $users = $wpdb->get_results("SELECT * FROM `WP_MAILLIST`;"); foreach($users as $user){ $compare = $wpdb->get_results("SELECT * FROM `wp_users` WHERE user_email='$user->user_email' LIMIT 1;"); if ( empty($compare) ) { $id=$user->id; #user doesent exist anymore $wpdb->query("UPDATE WP_MAILLIST SET user_registered='false' WHERE id=$id;"); } } } function sendmail_preview(){ global $wpdb; # get user options from table $option = $wpdb->get_results("SELECT * FROM `WP_MAILLIST_OPTIONS` LIMIT 1;"); # process options $option_sender=$option[0]->sender; $option_reply=$option[0]->reply; $option_title=$option[0]->title; $option_postsnumber=$option[0]->postsnumber; $option_postscats=$option[0]->postscats; $option_footer=$option[0]->footer; #process categories $option_cats = explode(",", $option_postscats); # get the post data $message=$_REQUEST['messagebox']; $message2=$message; $subject=$_REQUEST['subject']; $toself=$_REQUEST['toself']; $recent=$_REQUEST['recent']; $sendto=$_REQUEST['sendto']; #prepare message #validate $search[]='%URL%'; $replace[]='Site link'; $search[]='%NAME%'; $replace[]="User name"; $message2=str_replace($search,$replace,$message2); $message2 = preg_replace( "/(\n\r|\r\n|\r|\n)/", "
", $message2); # prepare message footer # Are we adding the recent articles? $recentart=false; if(!empty($recent)) { $recentart=true; //get recent articles and add to footer $first_run=true; $recent_query="SELECT DISTINCT * FROM wp_posts LEFT JOIN wp_post2cat ON wp_posts.ID = wp_post2cat.post_id"; if (!empty($option_cats)){ $recent_query.=" WHERE "; foreach ($option_cats as $cat) { if ($first_run==false){ $recent_query.=" OR "; } else {$first_run=false;} $recent_query.="category_id=".$cat; } } $recent_query.=" ORDER by post_date DESC LIMIT ".$option_postsnumber.";"; $recent = $wpdb->get_results("$recent_query"); $footer.="
".$option_title."

"; foreach ($recent as $rec) { $post=get_post($rec->ID); $title = $post->post_title; $excerpt=$post->post_excerpt; if (empty($excerpt)){ $excerpt=$post->post_content; $chars = 25; $text = $text." "; $text = substr($excerpt,0,$chars); $text = substr($text,0,strrpos($text,' ')); $text = $text."[..]"; $excerpt=$text; } $links= get_permalink($rec->ID); $links=strip_tags($links); $links=htmlspecialchars($links); $excerpt=strip_tags($excerpt); $excerpt=htmlspecialchars($excerpt); $title=strip_tags($title); $title=htmlspecialchars($title); $footer.=''.$title.'
'.$excerpt.'
'; } } # add the unsubcribe links $footer.="
".$option_footer." | Unsubscribe
"; #add footer to message body $message2.=$footer; #create array of the data to preview $mail['message']=$message; $mail['message2']=$message2; $mail['subject']=$subject; $mail['toself']=$toself; $mail['recent']=$recent; $mail['sendto']=$sendto; # return the preview return ($mail); } function sendmail() { global $wpdb; # get user options from table $option = $wpdb->get_results("SELECT * FROM `WP_MAILLIST_OPTIONS` LIMIT 1;"); # process options $option_sender=$option[0]->sender; $option_reply=$option[0]->reply; $option_title=$option[0]->title; $option_postsnumber=$option[0]->postsnumber; $option_postscats=$option[0]->postscats; $option_footer=$option[0]->footer; #process categories $option_cats = explode(",", $option_postscats); unset($footer); $toself=$_REQUEST['toself']; $recent=$_REQUEST['recent']; $sendto=$_REQUEST['sendto']; global $wpdb; global $sel; #get users from tables to deliver to if($sendto=='mail') { $mailinglist = $wpdb->get_results("SELECT * FROM `WP_MAILLIST` WHERE listed='true';"); } elseif($sendto=='reg') { $mailinglist = $wpdb->get_results("SELECT * FROM `WP_MAILLIST` WHERE user_registered='true';"); } else { $mailinglist = $wpdb->get_results("SELECT * FROM WP_MAILLIST INNER JOIN WP_MAILLIST_CACHE ON WP_MAILLIST.id = WP_MAILLIST_CACHE.user_id;"); } #get data to be sent $header=$_REQUEST['header']; $message=$_REQUEST['message']; $subject=$_REQUEST['subject']; #prepare emails and validate #first ready the header #prepare header (from) $header = "NFLdreams.com"; # write headers $reply_address="jolley_small@tesco.net"; $headers = "From: ".$option_sender." <".$option_reply.">\r\n" . "Reply-To: ".$option_reply."" . "\r\n" . "X-Mailer: PHP/" . phpversion() . "\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: text/html; charset=utf-8\r\n" . "Content-Transfer-Encoding: 8bit\r\n\r\n"; #prepare message body # validate subject and message setup $blogurl = get_bloginfo('url'); $blogname = get_settings('blogname'); $link = ''.$blogname.''; $message = preg_replace( "/(\n\r|\r\n|\r|\n)/", "
", $message); # prepare message footer # Are we adding the recent articles? $recentart=false; if(!empty($recent)) { //get recent articles and add to footer $first_run=true; $recent_query="SELECT DISTINCT * FROM wp_posts LEFT JOIN wp_post2cat ON wp_posts.ID = wp_post2cat.post_id"; if (!empty($option_cats)){ $recent_query.=" WHERE "; foreach ($option_cats as $cat) { if ($first_run==false){ $recent_query.=" OR "; } else {$first_run=false;} $recent_query.="category_id=".$cat; } } $recent_query.=" ORDER by post_date DESC LIMIT ".$option_postsnumber.";"; $recent = $wpdb->get_results("$recent_query"); $footer.="
".$option_title."

"; foreach ($recent as $rec) { $post=get_post($rec->ID); $title = $post->post_title; $excerpt=$post->post_excerpt; if (empty($excerpt)){ $excerpt=$post->post_content; $chars = 25; $text = $text." "; $text = substr($excerpt,0,$chars); $text = substr($text,0,strrpos($text,' ')); $text = $text."[..]"; $excerpt=$text; } $links= get_permalink($rec->ID); $links=strip_tags($links); $links=htmlspecialchars($links); $excerpt=strip_tags($excerpt); $excerpt=htmlspecialchars($excerpt); $title=strip_tags($title); $title=htmlspecialchars($title); $footeradd=''.$title.'
'.$excerpt.'
'; $footeradd=trim($footeradd); $footer.=$footeradd; } } #check if sending copy to self $mailself=false; if(!empty($toself)) { global $userdata; get_currentuserinfo(); $id=$userdata->ID; $self = $wpdb->get_results("SELECT * FROM `wp_users` WHERE ID=$id;"); foreach ($self as $val) { $addy=$val->user_email; # add the unsubcribe links $footer_self=$footer.'
'.$option_footer.' Unsubscribe
'; #add footer to message body $message_self=$message.$footer_self; # validate subject and message setup unset($search); unset($replace); $search[]='%URL%'; $replace[]=$link; $search[]='%NAME%'; $replace[]=$val->user_nicename; $subject_self=str_replace($search,$replace,$subject); $message_self=str_replace($search,$replace,$message_self); } $mailself=true; } $count=0; # send them to the maillist if ( !empty($mailinglist) ) { foreach ($mailinglist as $mail) { # add the unsubcribe links $footer_un='
'.$option_footer.' Unsubscribe
'; #validate #add footer to message body $subject_user=$subject; $message_user=$message; unset($search); unset($replace); $search[]='%URL%'; $replace[]=$link; $search[]='%NAME%'; $replace[]=$mail->user_name; $subject_user=str_replace($search,$replace,$subject_user); $message_user=str_replace($search,$replace,$message_user); $message_user=$message_user.$footer.$footer_un; $message_user=wordwrap($message_user,70); mail($mail->user_email, $subject_user, $message_user, $headers); $count++; }//end foreach } #send to self if ($mailself==true){ $message_self=wordwrap($message_self,70); mail($addy, $subject_self, $message_self, $headers); $count++; }//end mailself return($count); } function maillist_write(){ # This is the main part of the wordpress admin interface for maillist; the sending mail screen. # page formatting ?>
?page=maillist.php&sel=sel" method="post"> ?page=maillist.php" method="post">

?page=maillist.php&action=send&sel=sel" method="post"> ?page=maillist.php&action=send" method="post">

query("DROP TABLE WP_MAILLIST_CACHE;"); ?>

Messages Sent

A total of email messages were sent.

?page=maillist.php&action=preview" method="post"> ?page=maillist.php&action=preview&sel=sel" method="post">

Send an email to the mailing list

Available variables

  • %NAME% - User's full name
  • %URL% - Site's url/link
Message
Options
/>
/>
Send

get_results("SELECT * FROM WP_MAILLIST;"); foreach ($users as $user) { #get input from form if exists $current_post="check_".$user->id; $current=$_POST[$current_post]; if (!empty($current)){ //add to cache db $curr_id=$user->id; $wpdb->query("INSERT INTO WP_MAILLIST_CACHE (user_id) VALUES ($curr_id)"); $found=true; } } /////////////////////caching of users/////////////////////// return($found); } function maillist_admin(){ # This is the admin section for maillist ?>

Mailing List admin

Here you can change options to do with sending emails to the mailing list, and manage users*.

*note: It is recommended you do not delete users from the mailing list unless they are no longer registered, since they will be re-indexed.

Instead just set 'subscribed' to 'false'.

?page=maillist.php&action=adduser_done" method="post" name="mikeForm" onSubmit="return submitIt(this)" >

User details

get_results("SELECT * FROM `WP_MAILLIST`;"); foreach ($users as $user) { # check users table against new user if ($user->user_email==$user_email) { $exists=true; } }//end foreach if ($exists==false){ # add if not there $num = rand(100000, 999999); $result=$wpdb->query("INSERT INTO WP_MAILLIST (mail_key, user_name, user_email, user_registered, listed) VALUES ('$num', '$user_name', '$user_email', 'false', 'true')"); #check if successful if ( empty($result)){ echo '

Sorry, save failed

Go back

'; } else { echo '

New user saved

Go back

'; } } else { echo '

User not saved, email address already in Database

Go back

'; } break; case 'deleteuser': # delete users $found=cache_users(); if ($found==true){ echo '

Are you sure you want to delete the selected users?

[yes]

[no]

'; } else { echo '

No users selected

Go back

'; } break; case 'removeuser': # unsubscribe users $found=cache_users(); if ($found==true){ echo '

Are you sure you want to unsubscribe the selected users?

[yes]

[no]

'; } else { echo '

No users selected

Go back

'; } break; case 'subuser': # RE-subscribe users $found=cache_users(); if ($found==true){ echo '

Are you sure you want to Re-subscribe the selected users?

[yes]

[no]

'; } else { echo '

No users selected

Go back

'; } break; case 'sendmail': # send to selected $found=cache_users(); if ($found==true){ echo '

Are you sure you want to send email to the selected users?

[yes]

[no]

'; } else { echo '

No users selected

Go back

'; } break; case 'save': $set=$_POST['set']; $sender=$_POST['sender']; $reply=$_POST['reply']; $title=$_POST['titleb']; $postsnumber=$_POST['postsnumber']; $postscats=$_POST['postscats']; $footer=$_POST['footerb']; # CHECK if form submitted if (isset($set)){ # save options $wpdb->query("UPDATE WP_MAILLIST_OPTIONS SET sender='$sender', reply='$reply', title='$title', postsnumber='$postsnumber', postscats='$postscats', footer='$footer' WHERE id=1;"); echo '

Options saved

Go back

'; }else{ echo '

Options NOT saved, error encounted

Go back

';} break; case 'default': echo '

Are you sure you want to reset the options?

[yes]

[no]

'; break; case 'default2': $wpdb->query("DROP TABLE WP_MAILLIST_OPTIONS;"); echo '

Options reset

Go back

'; break; case 'delete': $users = $wpdb->get_results("SELECT * FROM WP_MAILLIST_CACHE;"); foreach($users as $user){ $wpdb->query("DELETE FROM WP_MAILLIST WHERE id=$user->user_id;"); } $wpdb->query("DROP TABLE WP_MAILLIST_CACHE;"); echo '

Users deleted

Go back

'; break; case 'unsub': $users = $wpdb->get_results("SELECT * FROM WP_MAILLIST_CACHE;"); foreach($users as $user){ $wpdb->query("UPDATE WP_MAILLIST SET listed='false' WHERE id=$user->user_id;"); } $wpdb->query("DROP TABLE WP_MAILLIST_CACHE;"); echo '

Users unsubscribed

Go back

'; break; case 'resub': $users = $wpdb->get_results("SELECT * FROM WP_MAILLIST_CACHE;"); foreach($users as $user){ $wpdb->query("UPDATE WP_MAILLIST SET listed='true' WHERE id=$user->user_id;"); } $wpdb->query("DROP TABLE WP_MAILLIST_CACHE;"); echo '

Users Re-subscribed

Go back

'; break; } # Write custom newsletter to mailing list } else { # Echo the users subscribed & options $options = $wpdb->get_results("SELECT * FROM `WP_MAILLIST_OPTIONS` LIMIT 1;"); ?>

Options

?page=maillist.php&action=save" method="post">

Email options

Recent article options

Footer options

Subscribed users

Add user

'; global $wpdb; # query mailing list $users = $wpdb->get_results("SELECT * FROM `WP_MAILLIST`;"); if (!empty($users)){ ?>
?page=maillist.php&doaction=submit" method="post">
user_name; ?> user_email.'">'.$user->user_email.''; ?> user_registered; ?> listed; ?>


Mailing list is empty

'; } } ?>