Jump to content

Activate sql trough javascript after next sql


funstad

Recommended Posts

Hey all,

 

I have a question about PHP SQL trigger from javascript

 

My situation:

- I have Swift-mailer (program to send mails trough php)

- I use this to send approx 1500 newsletters.

- Every +/- 100 newsletters my screen turns white, i have to refresh and click the send button again to send the remaining newsletters.

 

What i want to achieve:

- after sending 50 mails the sql queries stop (this is done by using the SQL command "LIMIT 50")

- I want then to let javascript kick in and wait for X time (example 5 min)

- After the 5 min it needs to restart the sql query by an automated command (without clicking on the send button)

 

Is this possible? or would you advise me to do this diffrent?

Please note: It has to be send trough a browser.

 

p7TVRbM.jpg

Link to comment
Share on other sites

That seems kind of hacky. If the screen is white then that sounds like a fatal error that you're not seeing because error messages are disabled, so I would start by enabling error reporting and error messages in php.ini, or if it's using an error log then check the log. It may be a timeout, maybe the maximum execution time is being hit. You can increase the time limit or set no time limit, although the browser may still time out (which wouldn't necessary stop the PHP).

 

Why do you have to use a browser? Is it not possible to use a cron job to automate that?

Link to comment
Share on other sites

What do you think about doing it this way?

Note: that this is only accessible for administrators who are logged in.

 

The reason it needs to happens trough a browser is because a non programmer needs to be able to create, save and send if fairly easy.

 

JS: run code every x-time (60 sec in example)

<script type="text/javascript">
setInterval(function(){
    document.forms["myform"].submit();
}, 60000);
</script>

Html code: with hidden input to send to php "timerdone"

    <form method="post" name="myform">
    <input type="hidden" name="timerdone">
    </form>

PHP code: see if the form is posted, and start query. (need to add a finish statement to stop the timer when all mails are send)

if(isset($_POST['timerdone'])){
 $msgtimer = 'Send: 50 mails send';
}
Edited by funstad
Link to comment
Share on other sites

Let me explain how we do that. Our users might need to send emails to tens or even hundreds of thousands of people. Those emails don't all go out at once, the admin isn't waiting for the entire batch to send. What they do is use our application (through the browser) to do things like select an email template and a distribution list of users, and then they press send. PHP will figure out the list of users in the distribution list, and will loop through them and build the template for each user and add the emails to a mail queue. Every minute we have a cron job execute a PHP script that checks the mail queue, and if there are any mails sitting there waiting to go out then it will start sending them. The admin doesn't wait for that to happen, the admin just queues up the emails and goes on with their day. The cron job that runs every minute is what actually sends out the emails that are waiting. The only tricky part is that we also have a way for that sending script to check if it is already running, because you don't want the PHP script to keep starting again every minute trying to send the same list of emails, so if it is already running then it can detect that and not duplicate everything.

 

For that, we use the Pear Mail and Mail_Queue packages, although those are fairly old at this point. I'm sure something like PHPMailer can also queue and send emails. But this doesn't require a browser window to stay open while the emails are being sent, the admin can go do whatever else they need to do in the mean time.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...