...

View Full Version : mail queue



the-dream
03-13-2007, 02:03 PM
i am creating an e newsletter application and want a mail queue.

how would this be done.

eg.

the mail has to be sent to 300 people it list who is left to send it to.

timgolding
03-13-2007, 02:15 PM
When you are ready to send your emails have your script create a stack or que of emails maybe as a sql table or something. If the table had a sent flag field then when a email is sent the sent flag for that record can be set to true. the stack/que should not be dropped until all the sent lags are set to true

P.s I strongly recommend using a cron job for this.

the-dream
03-13-2007, 02:21 PM
im sorry im a bit slow.

What!

timgolding
03-13-2007, 02:42 PM
have you done a SQL databse before?

the-dream
03-13-2007, 03:06 PM
yes all of the email addresses are stored in one and user ifo for the app it sored in anouther table with MD5 encription!

timgolding
03-13-2007, 03:42 PM
Ok try to turn the members table into a mailing list. Simply add an extra flag to confirm that user wishes to recieve mail. Then your script should run a query that selects all users with the recieve mail flag set to true. Once is has these records it should write them (or their key) to the stack. If you are intending to send all 300 emails in one sitting then it is exceptable to use an array. Then you can go through the stack one by one as you send an email remove it from the stack. Repeat until the stack is empty

the-dream
03-13-2007, 04:21 PM
Ok!

I will try!

aedrin
03-13-2007, 04:43 PM
If you are intending to send all 300 emails in one sitting then it is exceptable to use an array.

I would advise against having a script sit there for several minutes not providing any feedback, with the possibility of timing out (most likely).

Instead, create a temporary table that holds all of the email addresses, or keep an array in a session variable.

Have a page that refreshes itself after 1 second, every time the page loads, send the next X emails (X depends on the hosting server's processing power, to be safe you could do like 10). Then take those off of the list.

You can even display a progress bar so the user knows how far it is done.

And the only limit to the size would be the session data/temporary table size limit.

the-dream
03-13-2007, 04:47 PM
rite i am quite new to php
this is how it sends the email:


<?php

require_once('Connections/DreamMailer_db.php');
include('version/info.php');


$subject = $_POST['sub'];
$message = $_POST['mess'];
$fromEmail = "".$from."";

// sql: select email address
$sql = "SELECT info.Email FROM info";
$result = mysql_query($sql);
if (!$result){echo("Error performing query: " . mysql_error() . "");}

while ($query_data = mysql_fetch_array($r esult))
{
mail ($query_data['Email'], "".$subject."", "".$message."", "From: ".$fromEmail);
} // end while

?>


so

i want to create another page with a queue on it and i have tried to do it the way you guys said. but failed miserably. so if someone could point me in the right direction. that would be great. its not that they don't work its just i have no clue on how to do them!

timgolding
03-13-2007, 05:28 PM
So taking aedrins' advice we wil store them in a sql stack. First create a database table called email_stack with two fields; Email and Sent make sure that sent is default to false; Also remember to add the extra recieve_mail feild to the info table



<?php

require_once('Connections/DreamMailer_db.php');
include('version/info.php');


$subject = $_POST['sub'];
$message = $_POST['mess'];
$fromEmail = "".$from."";

// sql: select email address
$sql = "SELECT * FROM info";
$result = mysql_query($sql);
if (!$result){echo("Error performing query: " . mysql_error() . "");}

while ($query_data = mysql_fetch_array($result))
{
$email=$query_data['Email'];
$onlist=$query_data['recieve_mail'];// If this is true
if($onlist=true){
$q = 'INSERT INTO email_stack (Email) VALUES ('.$email.')';
$res = mysql_query($q)OR die(mysql_error());
}


} // So far we have made a list of emails to send it to. The script below shows how you can itterate through your stack and send the email.

// sql: select email address
$sql = "SELECT * FROM email_stack";
$result = mysql_query($sql);
if (!$result){echo("Error performing query: " . mysql_error() . "");}

while ($query_data = mysql_fetch_array($result))
{
$email=$query_data['Email'];
mail ($email, "".$subject."", "".$message."", "From: ".$fromEmail);
$sql='UPDATE email_stack
SET Sent = 1
WHERE Email = '.$email;
}



?>

aedrin
03-13-2007, 07:15 PM
timgolding, when you're posting code for someone, you don't have to add in all the error checking. This is something they can implement themselves, and it will only make it harder to read.

setup.php


$result = query("SELECT * FROM users WHERE send_mail = 1");
$emails = array();
while ($row = get_row($result)) {
$emails[] = $row['email'];
}

query("INSERT INTO mail_stack(email) VALUES('" . implode("'), ('", $emails) . "')";


mail.php


$result = query("SELECT * FROM mail_stack WHERE sent = 0");

if (num_rows($result)) {
$emails = array();
while ($row = get_row($result)) {
mail($row['email'], $subject, $body, $headers);
$emails[] = $row['email'];
}

query("UPDATE mail_stack SET sent = 1 WHERE email IN ('" . implode("', '", $emails) . "')");
}

the-dream
03-13-2007, 07:34 PM
what should be the default value of mail-stack?

aedrin
03-13-2007, 09:04 PM
What default value? The example we gave is a database implementation. It would be an empty table.

the-dream
03-13-2007, 11:50 PM
i really dont get any of this.?.?

aedrin
03-14-2007, 04:39 PM
Then perhaps you need to learn some more basic PHP. As this is quite basic stuff.

Perhaps that is why your advice is usually not helping the question.

StupidRalph
05-27-2007, 11:22 PM
timgolding, when you're posting code for someone, you don't have to add in all the error checking. This is something they can implement themselves, and it will only make it harder to read.

setup.php


$result = query("SELECT * FROM users WHERE send_mail = 1");
$emails = array();
while ($row = get_row($result)) {
$emails[] = $row['email'];
}

query("INSERT INTO mail_stack(email) VALUES('" . implode("'), ('", $emails) . "')");

-----------------------------------------------------------------------------------------------------------------^
mail.php


$result = query("SELECT * FROM mail_stack WHERE sent = 0");

if (num_rows($result)) {
$emails = array();
while ($row = get_row($result)) {
mail($row['email'], $subject, $body, $headers);
$emails[] = $row['email'];
}

query("UPDATE mail_stack SET sent = 1 WHERE email IN ('" . implode("', '", $emails) . "')");
}


No biggie, just seems you left off a parenthesis on the first implode function. I'm about to do something similar really soon.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum