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> </td> <td><input type=submit name=s1 value="Send" class="sub1"></td> </tr> </table> </form> <? require_once("admin_footer.php"); ?> |
Quote:
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. |
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. |
Hi
You could do it by changing this line: PHP Code:
PHP Code:
PHP Code:
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:
PHP Code:
|
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!!! |
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:
DELETE FROM class_members WHERE is_removed <> 0; Also remember that the start number starts with 0 not 1. |
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. |
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?
|
Yes, that's probably better. I didn't know you had MemberID.
PHP Code:
|
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! |
All times are GMT -5. The time now is 10:33 AM. |