Webocreation

Wednesday, February 3, 2010

Sending mail to many clients

<?php
include("connection.php");
if (!isset($_POST['submit'])):
?>

<html>
<head>
<title>Mailing List</title>
</head>

<body bgcolor="#D6DEE7">
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<table width="471" border="0">
<tr>
<td colspan="2"><b><h2 align="center">MAILING LIST ADMIN</h2></b></td>
</tr>
<tr>
<td width="143">Send message to:</td>
<td width="318"><select name="to" size="1" style="background-color: #F7F7F7">
<option selected value="all">Entire School
<OPTION value="primary">primary
<OPTION value="lower_secondary">lower secondary
<OPTION value="secondary">secondary
</select></td>
</tr>
<tr>
<td>Title or Subject: </td>
<td><input name="subject" type=text maxlength=100 size=40></td>
</tr>
<tr>
<td valign="top">Message:</td>
<td><textarea wrap name="message" rows=10 cols=45></textarea></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input type=submit name="submit" value="SUBMIT">
</div></td>

</tr>
</table>

</form>
</body>
</html>

<?php else:

$to = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$country = $_POST['country'];

if ("all" == $to) {
$x = 1;
$hold = 50; // quantity of emails sent before 3 sec delay
$emails = mysql_query("SELECT email FROM schooldetails");
while ($sendemail = mysql_fetch_array($emails)) {
$email = $sendemail["email"];
mail($email, $subject,
$message, "From:Your name <you@whatever.com>");

$x++;
if($x == $hold) { // When $x is equal to $hold, a 3 sec delay will occur avoiding php to timeout
sleep(3);
$x = 0;
}
} // end of while loop
echo "send to all";
}
elseif ("primary" == $to) {
$x = 1;
$hold = 50; // quantity of emails sent before 3 sec delay
$emails = mysql_query("SELECT email FROM schooldetails where type_id='1'");
while ($sendemail = mysql_fetch_array($emails)) {
$email = $sendemail["email"];
mail($email, $subject,
$message, "From:Your name <you@whatever.com>");

$x++;
if($x == $hold) { // When $x is equal to $hold, a 3 sec delay will occur avoiding php to timeout
sleep(3);
$x = 0;
}
} // end of while loop
echo "send to primary";
}
elseif ("lowersecondary" == $to) {
$x = 1;
$hold = 50; // quantity of emails sent before 3 sec delay
$emails = mysql_query("SELECT email FROM schooldetails where type_id='2'");
while ($sendemail = mysql_fetch_array($emails)) {
$email = $sendemail["email"];
mail($email, $subject,
$message, "From:Your name <you@whatever.com>");

$x++;
if($x == $hold) { // When $x is equal to $hold, a 3 sec delay will occur avoiding php to timeout
sleep(3);
$x = 0;
}
} // end of while loop
echo "send to lower secondary";
}
elseif ("secondary" == $to) {
$x = 1;
$hold = 50; // quantity of emails sent before 3 sec delay
$emails = mysql_query("SELECT email FROM schooldetails where type_id='3'");
while ($sendemail = mysql_fetch_array($emails)) {
$email = $sendemail["email"];
mail($email, $subject, $message, "From:Your name <you@whatever.com>");

$x++;
if($x == $hold) { // When $x is equal to $hold, a 3 sec delay will occur avoiding php to timeout
sleep(3);
$x = 0;
}
} // end of while loop
echo "send to secondary";
}
?>
<html>
<head>
</head>
<body>
SUCCESS!
</body>
</html>
<?php endif; ?>
Sending mail to many clients, sending mail to according to group

No comments:

Post a Comment