...

View Full Version : PHP and Cron Jobs. Mass emailer.



thesavior
04-27-2006, 05:22 PM
I have about 1500 emails in my database, I have a made a script that will just loop the mail() command for all of them, but I don't want the script to run for over 30 seconds, otherwise it stops. What i would like to do, is run them through a cron job, so the script doesn't need to be up, and i can send them in cycles of a number that i can change online. I have never created a cron job before, and i don't have cpanel. So how can i go about making a php script that will create a cron job, then doing it. Can i get some code examples here? Thanks alot.

firepages
04-28-2006, 03:53 AM
php probably wont be able to create the cron job for you since it would need elevated permissions to do so (unless you are lucky and apache or php appear in /usr/lib/cron/cron.allow).

if you have shell access use crontab to create the cron entry and simply point it at your php file ...

/usr/local/php -q -f /home/user/scripts/cronjob.php

+ remember to chmod a+x the cronjob.php

Nightfire
04-28-2006, 03:57 AM
You might need your host to start your crontab. Would email/phone them, as most don't like you using mass email from their servers

thesavior
04-28-2006, 04:01 AM
How do things like Invsion Power Board or VBulleting send out there mass emails? They are using cron jobs right? I want to do something like that.

Nightfire
04-28-2006, 04:05 AM
You mean the main company sites, or peoples personal sites? Personal sites won't use cron jobs, the main sites more likely will

missing-score
04-28-2006, 04:27 AM
They might use cron jobs but they also might just use an open SMTP connection. If you use mail() repeatedly the result will be very slow, for every e-mail the system opens a connection, sends the e-mail and then closes the connection.

With an open SMTP connection, the server opens the connection, sends every single e-mail through the same connection and then closes it.

Is this what you want, or do you specifically want to use a Cron job?

thesavior
04-28-2006, 05:30 AM
Im really just looking to do whatever way the have it running through Invision Power Board. You can mass email without a smtp connection, so im assuming it uses mail().

fci
04-28-2006, 01:30 PM
if invision is anything like vbulletin.. 'scheduled tasks' are not run via cron... they seem to be run when a user goes to a page and if it has reached a certain time it will run (I have not verified this but it wouldn't surprise me). I would say to use a *real* cronjob since then you are able to manage the email in a queue and contain the flow (listen to firepages).

MRMAN
04-28-2006, 01:39 PM
If the prroblem is that it is taking two long to complete the script then you can change the execution time for that script.

like this set_time_limit(3000) ;
the only prroblem is that if you are not outputting anything to the browser then the browser will look like the page has crashed. so you would need to print something to the screen every 20 seconds or so.

hope this helps

thesavior
04-28-2006, 02:24 PM
the problem is, i just don't want to make the admin sit there on the email page until it finishes. I want them to click send, then be able to do other things and know that the emails are being sent.

fci
04-28-2006, 02:27 PM
the problem is, i just don't want to make the admin sit there on the email page until it finishes. I want them to click send, then be able to do other things and know that the emails are being sent.

that is why you use a cronjob, it will be able to 'run in the background' ..

thesavior
04-28-2006, 02:38 PM
haha, so we are back to the beggining. Can i get some code to set up a cronjob to do this?

fci
04-28-2006, 02:41 PM
haha, so we are back to the beggining. Can i get some code to set up a cronjob to do this?

No, ask your sysadmin or do it via cpanel or whatever

thesavior
04-28-2006, 03:15 PM
haha, no because im making a forum system, and if i release it, i want it to set it up, not manually.

fci
04-28-2006, 03:18 PM
then you will have to write a scheduler that will be able to limit resources.

thesavior
04-28-2006, 03:21 PM
Can i get some help doing that. I am not so sure where to begin. Ive never dealt with this stuff before.

missing-score
04-28-2006, 03:36 PM
haha, no because im making a forum system, and if i release it, i want it to set it up, not manually.
Probably cant be done then... If you read what was said before, you would need PHP to have highly elevated permissions or allowed to create cronjobs (most PHP installations will not be able to).

thesavior
04-29-2006, 12:44 AM
ok, well, what I need is some solution to do this, and then help implementing it. I understand cronjobs won't work, but can I get help using a different thing?

chump2877
04-29-2006, 12:58 AM
If the problem is that you don;t want the person to just sit there doing nothing while the program sends massive numbers of emails, then why not open up a pop-up window to manage the task of sending e-mails, and leave the main window free so the person can continue to do whatever...


Edit:

And I might implement something like this:



If the prroblem is that it is taking two long to complete the script then you can change the execution time for that script.

like this set_time_limit(3000) ;
the only prroblem is that if you are not outputting anything to the browser then the browser will look like the page has crashed. so you would need to print something to the screen every 20 seconds or so.

in my pop-up window to let the person know that the emails are in the process of being sent....then when the emails are done sending, you can automatically close the pop-up window with some javascript perhaps (use output buffering to delay execution of javascript until after e-mail looping is done, to do this).

thesavior
04-29-2006, 01:30 AM
that still requires them to have a window open, which is unreliable. I want to make sure there is not a possibility to send them emails out to the beggining of the list and accidentally stop it, then send it again. I want it a way that the user doesnt have to do anything like on ipb

thesavior
04-30-2006, 06:43 AM
*bump*

thesavior
04-30-2006, 09:03 PM
*bump*

firepages
05-01-2006, 05:08 AM
that still requires them to have a window open, which is unreliable. I want to make sure there is not a possibility to send them emails out to the beggining of the list and accidentally stop it, then send it again. I want it a way that the user doesnt have to do anything like on ipb

checkout ignore_user_abort() (http://www.php.net/ignore_user_abort)
which might allow the popup window to run the script which will continue even if the admin closes the popup.

missing-score
05-01-2006, 05:24 AM
I think what you are looking for is quite unrealistic (in a way) for download/install type scripts, on the one hand, you dont want your user to accidentally send twice but you also dont want to make them wait at all.

Surely keeping the user hanging for 30 seconds or so telling them with a message like "We are sending messages, this could take a while depending on how many people you are e-mailing. Sometimes, this process may take over 2 minutes, do not reload the page otherwise your e-mails will be sent twice." or something including those points would be better than "were doing this but you cant see it, so just trust us that it worked"...

I think the ignore_user_abort() is your best bet, but it may not get you exactly what you want.

thesavior
05-01-2006, 08:33 PM
ok, well i guess i would even be happy with an ajax thing that says how far complete the sending is. Would this be possible?

thesavior
05-01-2006, 08:36 PM
What about a popup that shows a list of people that it is sending the email to, then a javascript checkmark that shows that it has been sent to them, and a total percent br at the top. Could this be done with ajax?

GJay
05-01-2006, 08:40 PM
'yes'

thesavior
05-02-2006, 01:30 AM
okay. Any code snippets or help that would get me along with doing that?

thesavior
05-03-2006, 03:55 AM
*bump*



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum