Jump to content

PHP Newsletter script help


Shade_tree_Dude

Recommended Posts

Where to begin? I have a very successful newsletter script for PHP-Fusion 7.02.xx CMS system. Currently it will send to single or multiple users w/attachments; also will send to a user group w/attachments. I have a need/request from a user to add multiple user group capability. Well sending to 1 user group is no problem. User groups are stored in the 'users' table like this: Say a user belongs to 3 user groups, 1 3 and 4. the 'user_groups' field for his record in the db looks like this:

.1.3.4

Below is the usergroup select code and the email addies extraction code according to usergroup, that as I said currently works fine:

// Select user group
	echo "<table cellspacing='0' cellpadding='0' border='0'><tr>\n";
	echo "<td valign='middle' align='left' width='300'><input type='radio' name='delivery' value='grp'".$grp."> ".$locale['nl_447']."</td>\n";
	echo "<td align='center'>\n";
	echo "<select name='sendto' size='6' class='textbox' style='width:140px;'>";
	$result = dbquery("SELECT * FROM ".DB_USER_GROUPS."");
	if (dbrows($result) != 0) {
	while ($data = dbarray($result)) {
            $selected = (isset($_GET['group_id']) && $_GET['group_id'] == $data['group_id']) ? 'selected="selected"' : '';
            echo "<option value='".$data['group_id']."'$selected>".$data['group_name']."</option>\n";
          }
}
	echo "</select>\n";
	echo "</td>\n</tr></table><br /><br /></td>\n";
	echo "</tr><tr>\n";
// End select user group



// Get usergroup and email addresses
	$subject = stripslashes($_POST['subject']);
	$frommail = $settings['siteemail'];
	$usrtxt = stripslashes($_POST['msg']);
	$format = $_POST['format'];
	$grID = $_POST['sendto'];
	$result = dbquery("SELECT user_email FROM ".DB_USERS." WHERE user_newsletter='1' AND user_groups REGEXP('^\\\,{$grID}$|\\\,{$grID}\\\,|\\\,{$grID}$')");
	if (dbrows($result)) {
		$rows = dbrows($result);
	while ($data = dbarray($result)) {
		$addies = $data['user_email'].',';
	$my_array = explode(",",$addies);
foreach($my_array AS $dudes) {
$prep = $dudes;
	$array[] = $prep;
	$list = implode(",",$array);
	$to_mail = $list;
	$list_array = explode(",",$to_mail);
		}
	}
}
	reset($list_array);
// End get usergroup

I think I will still have to use REGEX to send to multiple usergroups but have no idea how to go about it. Any help would be appreciated.

Link to comment
Share on other sites

Ah, the joys of denormalized databases.

"REGEXP '\.{$grID}(\.|$)'"
First a period, then the number, then either another period (another group follows) or the end of the list.

 

[edit] Your example uses periods but your regex uses commas?

Edited by requinix
Link to comment
Share on other sites

Darn it! No, the REGEX uses periods. I was trying different things to make multiple select work and normalized the code before I copied and pasted into the code block. But I forgot to return the REGEX to periods, sorry; my mistake. That code uses periods and it DOES work flawlessly; unfortunately I cannot now edit the post.

Link to comment
Share on other sites

So if it works then what's the problem?

 

unfortunately I cannot now edit the post.

You've got a few minutes after you post to make edits, but beyond that you should make another reply. Helps make sure that people don't see one version of your post, start replying to it, and then in the middle you go back and make some important change that totally screws up what they were going to say.
Link to comment
Share on other sites

Guess maybe I didn't make myself clear. YES it works just fine as is for sending to a single usergroup; I need to modify the code to be able to send to the users of multiple usergroups. Here is the corrected code; ignore the code in the first post as the REGEX had not been changed back to periods:

// Select user group
	echo "<table cellspacing='0' cellpadding='0' border='0'><tr>\n";
	echo "<td valign='middle' align='left' width='300'><input type='radio' name='delivery' value='grp'".$grp."> ".$locale['nl_447']."</td>\n";
	echo "<td align='center'>\n";
	echo "<select name='sendto' size='6' class='textbox' style='width:140px;'>";
	$result = dbquery("SELECT * FROM ".DB_USER_GROUPS."");
	if (dbrows($result) != 0) {
	while ($data = dbarray($result)) {
            $selected = (isset($_GET['group_id']) && $_GET['group_id'] == $data['group_id']) ? 'selected="selected"' : '';
            echo "<option value='".$data['group_id']."'$selected>".$data['group_name']."</option>\n";
          }
}
	echo "</select>\n";
	echo "</td>\n</tr></table><br /><br /></td>\n";
	echo "</tr><tr>\n";
// End select user group



// Get usergroup and email addresses
	$subject = stripslashes($_POST['subject']);
	$frommail = $settings['siteemail'];
	$usrtxt = stripslashes($_POST['msg']);
	$format = $_POST['format'];
	$grID = $_POST['sendto'];
	$result = dbquery("SELECT user_email FROM ".DB_USERS." WHERE user_newsletter='1' AND user_groups REGEXP('^\\\.{$grID}$|\\\.{$grID}\\\.|\\\.{$grID}$')");
	if (dbrows($result)) {
		$rows = dbrows($result);
	while ($data = dbarray($result)) {
		$addies = $data['user_email'].',';
	$my_array = explode(",",$addies);
foreach($my_array AS $dudes) {
$prep = $dudes;
	$array[] = $prep;
	$list = implode(",",$array);
	$to_mail = $list;
	$list_array = explode(",",$to_mail);
		}
	}
}
	reset($list_array);
// End get usergroup
Link to comment
Share on other sites

1. Rename the

2. $grID will now be an array.

3. Change the regex to (be a little simpler and) look like

'\\\.(id 1|id 2|id 3|...)(\\\.|$)'
If the IDs selected were 1, 10, 15, and 99 then the regex would be

'\\\.(1|10|15|99)(\\\.|$)'
You can basically implode() the $grID but you still need to validate that each thing is a number.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.