LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   PHP Script Limit Emails Being Sent?? (https://www.linuxquestions.org/questions/linux-newbie-8/php-script-limit-emails-being-sent-4175509860/)

wendy531 07-02-2014 08:48 AM

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");
?>

TB0ne 07-02-2014 09:09 AM

Quote:

Originally Posted by wendy531 (Post 5197338)
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.

wendy531 07-02-2014 09:22 AM

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.

Guttorm 07-02-2014 12:34 PM

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;
  } 


wendy531 07-02-2014 12:58 PM

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!!!

Guttorm 07-02-2014 01:07 PM

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.

wendy531 07-02-2014 01:13 PM

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.

wendy531 07-02-2014 01:15 PM

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?

Guttorm 07-02-2014 01:20 PM

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.

wendy531 07-02-2014 01:44 PM

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.