LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-02-2014, 09:48 AM   #1
wendy531
LQ Newbie
 
Registered: Jul 2014
Posts: 6

Rep: Reputation: Disabled
PHP Script Limit Emails Being Sent??


Hi Everyone,

I am new to this site and hope I am posting in the right area. I have a website and I need help with some coding. I need to limit the amount of newsletters that are sent out to my registered users.

Can someone tell me how to add a line or so in this script to maybe choose sending to members 1 - 200 and then I could change to at any time to send it to different member numbers?

I really appreciate any information that is given. Thank you so much for your time.

Here is the newsletter script:

<?
require_once("../conn.php");
require_once("access.php");
require_once("AdminNavigation.php");

if(isset($_POST[s1]))
{
$q1 = "select * from class_members";
$r1 = mysql_query($q1) or die(mysql_error());

while($a1 = mysql_fetch_array($r1))
{
$to = $a1[email];
$subject = $_POST[sub];
$message = $_POST[MyMessage];

$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "From: $_SERVER[HTTP_HOST] <$aset[ContactEmail]>\n";
$headers .= "X-Priority: 1\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "X-Mailer: PHP/" . phpversion()."\n";

mail($to, $subject, $message, $headers);

$i++;
}

$error = "$i messages was sent";
}

//get the number of registered members
$q1 = "select count(*) from class_members";
$r1 = mysql_query($q1) or die(mysql_error());
$a1 = mysql_fetch_array($r1);

if($a1[0] == 0)
{
echo "<br><br><center>There are no registered members, yet!</center>";
exit();
}

?>

<script>
function CheckMail() {

if(document.f1.sub.value=="")
{
alert('Enter the newsletter subject line, please!');
document.f1.sub.focus();
return false;
}

if(document.f1.MyMessage.value=="")
{
alert('Enter the newsletter text, please!');
document.f1.MyMessage.focus();
return false;
}

}
</script>

<br><br>
<form method=post onsubmit="return CheckMail();" name=f1>
<table align=center width=500>

<tr>
<td></td>
<td><b>Newsletter</b><br><?=$error?></td>
</tr>

<tr>
<td align=right>Subject:</td>
<td><input type=text name=sub size=36></td>
</tr>

<tr>
<td align=right valign=top>Message:</td>
<td><textarea name=MyMessage rows=10 cols=35></textarea></td>
</tr>

<tr>
<td>&nbsp</td>
<td><input type=submit name=s1 value="Send" class="sub1"></td>
</tr>

</table>

</form>

<?
require_once("admin_footer.php");
?>
 
Old 07-02-2014, 10:09 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,916

Rep: Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689
Quote:
Originally Posted by wendy531 View Post
Hi Everyone,
I am new to this site and hope I am posting in the right area. I have a website and I need help with some coding. I need to limit the amount of newsletters that are sent out to my registered users.

Can someone tell me how to add a line or so in this script to maybe choose sending to members 1 - 200 and then I could change to at any time to send it to different member numbers? Here is the newsletter script:
Code:
<?
require_once("../conn.php");
require_once("access.php");
require_once("AdminNavigation.php");

if(isset($_POST[s1]))
{
	$q1 = "select * from class_members";
	$r1 = mysql_query($q1) or die(mysql_error());
			
	while($a1 = mysql_fetch_array($r1))
	{
		$to = $a1[email];
		$subject = $_POST[sub];
		$message = $_POST[MyMessage];

		$headers = "MIME-Version: 1.0\n"; 
		$headers .= "Content-type: text/plain; charset=iso-8859-1\n";
		$headers .= "Content-Transfer-Encoding: 8bit\n"; 
		$headers .= "From: $_SERVER[HTTP_HOST] <$aset[ContactEmail]>\n"; 
		$headers .= "X-Priority: 1\n"; 
		$headers .= "X-MSMail-Priority: High\n"; 
		$headers .= "X-Mailer: PHP/" . phpversion()."\n"; 

		mail($to, $subject, $message, $headers);

		$i++;
	}

	$error = "$i messages was sent";
}

//get the number of registered members
$q1 = "select count(*) from class_members";
$r1 = mysql_query($q1) or die(mysql_error());
$a1 = mysql_fetch_array($r1);

if($a1[0] == 0)
{
	echo "<br><br><center>There are no registered members, yet!</center>";
	exit();
}

?>

<script>
	function CheckMail() {

		if(document.f1.sub.value=="")
		{
			alert('Enter the newsletter subject line, please!');
			document.f1.sub.focus();
			return false;
		}

		if(document.f1.MyMessage.value=="")
		{
			alert('Enter the newsletter text, please!');
			document.f1.MyMessage.focus();
			return false;
		}

	}
</script>

<br><br>
<form method=post onsubmit="return CheckMail();" name=f1>
<table align=center width=500>

<tr>
	<td></td>
	<td><b>Newsletter</b><br><?=$error?></td>
</tr>

<tr>
	<td align=right>Subject:</td>
	<td><input type=text name=sub size=36></td>
</tr>

<tr>
	<td align=right valign=top>Message:</td>
	<td><textarea name=MyMessage rows=10 cols=35></textarea></td>
</tr>

<tr>
	<td>&nbsp</td>
	<td><input type=submit name=s1 value="Send" class="sub1"></td>
</tr>

</table>

</form>

<?
require_once("admin_footer.php");
?>
You need a "for" loop in there, which is well documented in the PHP documentation:
http://www.php.net/manual/en/control-structures.for.php

You could make a new function called "sendMail", and loop through it, using the mail command you already have. You don't say whether you're calling this from the command line or web page, and how the user is going to input the number of emails to send, so it's hard to say more.
 
1 members found this post helpful.
Old 07-02-2014, 10:22 AM   #3
wendy531
LQ Newbie
 
Registered: Jul 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thank you for your quick response. I am still a bit confused, I am more of an html person so this scripting has me very confused.

I copied that entire page for you to see. So could you possibly tell me the exact line I would need to add.

I would be the one entering the number of emails sent. So it sounds like the "for" loop would work and I could just go edit the line to change the numbers.

The website is pupclassifieds.com so the breeders are registered to receive newsletters from me. However I can only send 1500/day according to my hosting company even if they have said yes to receiving my newsletter. So that is where I am stuck trying to send out the letter when I now have 2500 members.
 
Old 07-02-2014, 01:34 PM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,261

Rep: Reputation: 297Reputation: 297Reputation: 297
Hi

You could do it by changing this line:
PHP Code:
$q1 "select * from class_members"
This will send to the first 1400:
PHP Code:
$start_at 0;
$send_count 1400;
$q1 "select * from class_members order by email limit $start_at$send_count"
Then the next day:

PHP Code:
$start_at 1400;
$send_count 1400;
$q1 "select * from class_members order by email limit $start_at$send_count"
But there will be problems when the list is changing. Can the list change between the days?

If it does, you can let the script run, and just sleep till the next day when the limit is reached.

In the beginning of the script:
PHP Code:
ignore_user_abort(true); // So the script will continue running when the browser is closed
$send_counter 0
Then in the beginning of the loop:
PHP Code:
while($a1 mysql_fetch_array($r1))
{
  if (++
$send_counter == 1400) {
    
sleep(24*60*60);
    
$send_counter 0;
  } 
 
1 members found this post helpful.
Old 07-02-2014, 01:58 PM   #5
wendy531
LQ Newbie
 
Registered: Jul 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
That sounds easy enough! The list would only change if someone removes themself. But that shouldn't matter if I just set it to send to members 1 - 1400, then it would only send up to 1400 messages in a day...correct?

I am going to try this!

Thank you for your help. Will keep you posted with the progress!!!
 
Old 07-02-2014, 02:07 PM   #6
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,261

Rep: Reputation: 297Reputation: 297Reputation: 297
Yes, but if a person is among the first 1400 and is removed from the list, number 1401 would be skipped. The only way I can think of to prevent this, is to change it so removed recipients are not deleted. You could maybe add a column like is_removed and check it in the PHP script. Before sending mail:

PHP Code:
if ($a1['is_removed']) {
  continue;

Set the default value of the column to 0, and change it to 1 when people unregister. And when you know there is no sending in progress you can delete the rows where is_removed <> 0

DELETE FROM class_members WHERE is_removed <> 0;

Also remember that the start number starts with 0 not 1.
 
Old 07-02-2014, 02:13 PM   #7
wendy531
LQ Newbie
 
Registered: Jul 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Update.....

I just ran the script and it did just as you said it would!!! It only sent out 1400 emails. Now hopefully when I run it tomorrow it will send to the next 1400 hundred and not the same people.

Thank you so much for such an easy fix. I have been researching for over a month.
 
Old 07-02-2014, 02:15 PM   #8
wendy531
LQ Newbie
 
Registered: Jul 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Is there a way to have it just send to MemberID 1 thru MemberID 1400 maybe? That way when they remove themselves it still just sends to all memberids in that string?
 
Old 07-02-2014, 02:20 PM   #9
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,261

Rep: Reputation: 297Reputation: 297Reputation: 297
Yes, that's probably better. I didn't know you had MemberID.

PHP Code:
$q1 "select * from class_members WHERE MemberId >= 1 AND MemberId <= 1400"
But now a mail is sent to the first 1400 in alphabetical order of email, so I'd wait and change it next time.
 
Old 07-02-2014, 02:44 PM   #10
wendy531
LQ Newbie
 
Registered: Jul 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Great. Sorry I didn't explain it better. I will change it and try that tomorrow.

I really appreciate all your help!

Thank you so much!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sendmail queue group limit emails mario.almeida Linux - Server 0 04-09-2013 02:44 PM
Mutt + Offlineimap: how to limit fetching to 10 last emails only Xeratul Linux - General 1 01-23-2013 03:35 PM
Integration of Postfix/SpamAssassin in order to limit the # of emails sent per hour gacanepa Linux - Newbie 1 11-13-2012 02:59 PM
limit number of emails send from one IP ridwan77 Linux - Security 1 05-20-2009 04:52 AM
Limit amount of emails on server ganninu Linux - Networking 7 07-30-2003 12:21 PM


All times are GMT -5. The time now is 01:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration