is cron job what should be used in this case? - php

Here is the 'use case':
Admin goes to the newsletter.php, fills in the form for email, i.e the subject, group of users to whom email is sent, writes the message and clicks the "Send" button.
Problem:
The number of emails sent per hour should be limited to, let's say, 400. That is, one email should be sent approx. every 10 seconds. Besides, sent and not sent emails should be tracked.
Question:
Will cron job do the trick?
The code is written in Yii framework. Is it possible that cron job will be activated when the user clicks on the "Send" button or only in the command line?
If cron job can do things above, can it be activated only in specific action of specific controller? or it affects the whole script?
Thank you

1- Yes it is a cron job.
2- You can do it in Yii refer to ConsoleCommand. Don not know how much do you know about cron? but make a script using consolecomamnd which runs every 10 secs, gets a singe email address in the queue and sends email to that address + removes it from the queue.
3- Yes in an action just build a queue (hint: mysql table) with the email address you need

I think you have the concept of a cron job wrong.
Think of the process this way:
Admin presses the send button.
...That script creates a queue i.e. adds email address's etc to a database table lets call it Queue. And finishes.
You setup a cron job to run every 5 minutes for example.
...The cron starts a PHP script which processes X entries from the Queue table, sends those emails, removes the entries from the queue table, and stops.
...The cron job starts again automatically after 5 minutes and repeats the process...
All you have to do is work out how many emails to send in each execution of the cron job, and how often to run it, so you dont exceed your limits or get flagged as a spammer.

My suggestion is that if you wanted to use cron to do such a thing, you would want your newsletter.php to write out a file to disk or database which contains the list of users. You would write a simple PHP script which would be triggered by cron which would be responsible for calling sendmail to send the messages. As each recipient is mailed you script would remove them from the list.
Alternately, you might want to look into some basic mailing list software. These often support a notion of throttled message sending.
I assume you are trying to manage limits imposed by your hosting provider related to number of emails sent per hour (antispam controls)

Yes cron jobs can be used in such a scenario. I personally have developed newletter systems which use such methodology. However, I send 350 and do a cron job every hour.
You should check if there are any emails to be sent on the first line and leave the cron job running every hour. There is no need to activate the cron job when the send button is clicked

cron itself just runs programs on a schedule. It doesn't have any logic in it that can know whether somebody clicked on a button, or logic that can know how many emails you've sent this hour, or which emails have or have not been sent.
cron runs under the control of a daemon. Although you probably could enable and disable it through a controller, you probably don't want to do that. If you're using cron, you usually need it to run all the time.
Instead, put the logic and the constraints into the program that cron runs.

if you can use system or exec function you could call cron job or any thing else in terminal can do it
for example system("ps aux | grep crond");

Related

Scheduled email in php

I have a program written in php/html where the user will input a job then after submit, will notify the admin and other users through email. What I wanted is to have a scheduled email where it will notify the admin/users that the job has not yet been done after 12 hrs. I've read about cronjob or cron tab but I don't understand hoe to use it well. Is there another way to do it using phpmailer and not crontab?
yes and no.
PHP by its nature is a stateless script that runs when a user or service requests a url.
The php script runs, does it stuff and then ends, normally there is relatively short timeout either in the PHP config or on the webserver, so you cant have a script running in the background waiting for a specific time to send out your email.
In your situation, you would create a cron job (or a scheduled task on windows) this task/job generally runs on the webserver (although is doesn't have to), the task will load a php script, either as a command or via wget.
So set the task to start at the intervals you require, it will envoke the PHP script, send the emails or what ever you want it to do then the php script will end, ready to run on the next schedule.

How to automatically call a php script every day to send out automatic emails

Is there a way where I can automatically call a php script after a specified interval.
I have a php script(say remindusers.php) that uses mysql to query a database where people have submitted their weekly reports. This script automatically queries the database and sends an email reminder to people who have not sent in their weekly reports yet.
What I am now supposed to do is give the ADMIN an option to set a reminder start and reminder end date during which calls should be automatically made to my remindusers.php script and cease on reminder end date.
What I learnt from SO/google is that I can setup cron (in LINUX) to automatically call my remindusers.php, but I dont have any shell access to do this.
Else Can I write another php script to essentially sleep every 24 hours and automatically wake up to call my remindusers.php script.
Are there any other built-in methods ?
Any ideas?
Use your site's visitors to trigger the event. Send a message and then check if 1 day elapsed. Then send another. You still need to pay attention not to double/triple/... send deu to synchronization.
When the time has elapsed use a MySQL (or system) MUTEX to ensure only one send occurs.
Yes you can! What you need to do is to use cron jobs. Cron jobs are essentially telling the server to execute a script (PHP or otherwise) at regular intervals. Cron jobs are very powerful and customizable, as you can set virtually any interval for your cron.
If you are using CPanel to manage your site, there is a button in CPanel to view all the cron jobs you have set. There is also a tutorial on that page.
Hope this helps.
Try with this PHPCron
PHPCron is a simple PHP script which lets you run multiple tasks on a schedule or timer. It can be run either from the command-line or via a web browser. Its behaviour is very similar to the popular cron program for UNIX.
http://katyscode.wordpress.com/2006/10/17/phpcron-running-scheduled-tasks-from-php-on-a-web-server/
I understand that you don't have Shell access but have you had a look at the cPanel to see whether there is an option to setup a cron job in there?

Configure Scheduled Task in wamp Server [duplicate]

I want to schedule sending of email from php script. I want user to specify date and time and then on the specified date and time, I want those emails to be sent automatically. How do I implement it? I am working on codeigniter.
One way to do it would be to create a "scheduled_emails" database table. Put all the emails you want to queue in there, including columns such as, recipient, subject, message and optional headers.
You could then set up a script to look at that table and send any emails that have a "send_time" which is greater than the current time. You could then set up a cron job to run this script every.. 5 minutes for example.
PHP usually uses an external scheduler for this sort of thing. That means cron on *nix or Windows Task Scheduler on Windows.
If you want to set it up through a web interface, then you might consider storing your schedule in a database and having cron (etc) kick off a script that looks for overdue emails every 5 minutes.

Send mails with PHP regularly at a specific time

I have three mysql tables; Users, Tasks and Task_Users, where each user can create their tasks through a web application I'm developing in PHP.
The thing is, I want my application to send an email to the users one day before the task occurs. The first thing that came to my mind was to do a MySQL job or trigger to send the email but I dont have an idea of how to do id,
What would be the best way make this happen or how could I call a job through PHP and execute it every day and verify that there is just one day left before the taks ocurrs and then if that is true send the email?
Here's my table definition. Thanks in advance.
Users
----------
UserId,Names,email,pass
Tasks
------------
TaskId,TaskName,Descripcion,TaskDateTime
Tasks_Users
------------
TaskId,UserID
You can write a CRON Job for this.
What's CRON Job ?
Cron is the name of program that enables unix users to execute commands or
scripts (groups of commands) automatically at a specified time/date. It is
normally used for sys admin commands, like makewhatis, which builds a
search database for the man -k command, or for running a backup script,
but can be used for anything. A common use for it today is connecting to
the internet and downloading your email.
What do you have to do ?
Just write a normal PHP script. make one that will work if it's launched directly from the browser. Then schedule that very same PHP file to run in cron, using this as a guide:
How to run a CRON Job after it's written ?
http://www.inmotionhosting.com/support/edu/cpanel/301-run-cron-job
If you have any issues let me know.
I'd make a cron job that regularly runs a script (say every 5min?) which checks for tasks that need emails sent. Then the script script sends the necessary emails.
*/5 * * * * php /path/to/script.php
Would call script.php, which checks and sends emails, every 5minutes.
Some info on cron is here:
http://www.pantz.org/software/cron/croninfo.html

Send mails in background without cron

I was wondering if there is a way to run a PHP loop in order to send a few hundred emails to subscribers in background. My goal is to format the newsletter, click send and then close the browser or change page. Of course, the actual process of sending the e-mail will be running in background and would not be interrupted by the browser closing.
I know this can be made with a cron job reading from a queue saved in MySQL or text file, but this way, even if there is no queue for a long period, the cron will always be running, looking for the queue...
I've seen this funcionality in a script called Pommo (https://github.com/soonick/poMMo) but can't seem to understand how it's done.
Does anyone have an idea for this?
I was going to add a comment to your question, but them I didn't have enough space there to format and give the example.
Here is an idea I believe might work:
1 - Load all the emails you want to send to a database or file.
2 - From your web application click on the button to send emails. This will submit an Ajax request to the server. You can define the number of emails you want to send within a certain timeframe. Remember that most hosts have limits on number of emails you can send every hour.
3 - create a php script that will receive the Ajax request and send all the emails within the parameters you define.
4 - I believe you can kill your web browser because the PHP script will run through the whole list and will not return until it finishes sending all the emails.
The above might work, however I would never do it this way. I would use a cronjob as stated above. Your cronjob would only have to check if there are emails to send or not. This is not resource intensive.
If you decide to implement the ideas above, please make sure you let us know. I am curious if that would work.
Good luck!
I know this can be made with a cron job reading from a queue saved in
MySQL or text file, but this way, even if there is no queue for a long
period, the cron will always be running, looking for the queue...
That pretty much beats the purpose of Cron. You should create a job that runs, say, every 15 minutes and checks the queue for mails that need to be sent. If there are no mails, let the script die, it'll run again in 15 minutes.
If there are mails to be sent, update the rows to indicate that you're processing them before you start sending, so a run taking more than 15 minutes won't cause another script instance to send the same mails.
You need a queue system. There is e.g. Beanstalkd for linux, which you would feed things with php.

Categories