I am new with laravel. I need to send email every day, for example - at 10pm. I know how to send email, but I can't figure out, how to send this email, or lunch my email sending function every day, at specific time.
This probably isn't as much to do with Laravel as it is a task scheduler.
Check out Crontab for running scripts on a schedule
http://crontab.org
For running a job every day at 1pm Monday through Friday.
0 13 * * 1-5 $HOME/scripts/weekday_email.php
Laravel does have a 'later' method for the Mail class, but it won't send email repeatedly.
Mail::later(5, 'emails.welcome', $data, function($message)
{
$message->to('foo#example.com', 'John Smith')->subject('Welcome!');
});
http://laravel.com/docs/mail
The best way to do this in laravel is to build an artisan command, and then call that command via a cron job like so:
1 * * * * /path/to/php /path/to/app/artisan command:name
This allows you to easily test the cron by running the command directly in the terminal. Apart from that, using Laravel artisan commands means that you don't have to worry about autoloading all the classes necessary to execute the command - Laravel will handle that for you.
php artisan command:name
Related
callapi file (command file)i am new to php and got an assignment in laravel nova.
i need to update my database every hour. I've manged to do it manually but i cant seem to make it work in laravel task scheduler. this is the appropriate files (i hope). thanks in advance for the answers.
1.i created a new command
2. i add the command logic
3. i set the command to run every minute (for testing) in kernel.php
4. i ran php artisan schedule:run.
no response!
kernel.php file
command shows on php artisan list
I need to run a scheduler task from blade view by clicking a button (Sync) and it should go for process in the background.
I have created an artisan command that is php artisan projects:get and then I schedule it once a day for running in cron job, but in some case we need to run the cron job at user's choice so when he/she is in CMS logged in, they can click a sync button to run the cron job from there, but I think its not possible, but I knew there is some work around in Laravel to process the php artisan command which I already created that is projects:get using queues or process from symphony, but I know I can do it from the command line (terminal) using putty or cPanel terminal window, but As you know client can't login into cPanel and run the command so we need to give them just a simple button to click and sync in background, right now when user click that button, its getting delay and he/she can't continue to work on other things while its fetching all the projects from the API's that I used in that command. We need to queue/background process.
php artisan projects:get
As you pointed out, you can run artisan commands from your php code as documented in the documentation
Since the artisan command will likely take some time to execute, it is a good practice to use a queue for this.
You said in the comments, that you are on xampp. On local, you need to run the pap artisan queue:work command once you started xampp. After you executed the command, the watcher will pick up jobs and will execute them. However, first you need to configure the queue. This will get you up and running. On a production server, you need to configure a supervisor to run the queue command.
You can run artisan command programmtically like this:
Route
Route::get('/run/command', 'SomeController#runCommand');
SomeController
public function function()
{
$exitCode = \Illuminate\Support\Facades\Artisan::call('projects:get');
return $exitCode;
}
I have problems setting up a cron using CodeIgniter. I've followed the documentation and set up a test cron
* * * * * php /home/USERN/public_html/spider/index.php tools message
But this doesn't work. The output is just the index.php default controller, and not tools/message. When I run it in the terminal on the server, I get the results that I expect. Is there something I am doing wrong, or do I need to change something on the server?
For cPanel servers, in order to for CI to use the URI segments right, You'll have to use
/usr/local/bin/php
I've been searching for a while and think I have part of the information I need but just need some assistance putting it all together.
What I'm trying to achieve is to call a URL (a codeigniter controller) on a regular basis e.g. every 5 minutes which will go through my database mail queue and send the mail using amazon SES.
So far I have successfully created the controller, model, DB and SES is working just fine. The controller sends 10 emails at a time and it all works fine when I manually hit the URL.
I'm not too familiar with cron jobs, but think this is where I need to head.
My application is set up on Elastic beanstalk on AWS.
I think that I need a folder called .ebextensions in my web root, with a file called something.config in it, where I can put some 'container commands'. I also think I will need to include 'leader_only: true' in there somewhere to avoid my replicated instances doing the same jobs.
When I don't understand is what should my container command be, considering controller is 'http://myapplication/process_mail' ? From examples I've seen I couldn't see how it determines the frequency, or even the code that 'calls' the URL.
In my controller, I previously had the following code to ensure it could only be called from the command line. Is this something I can keep and have or will the container command just hit the URL like any other user?
if (!$this->input->is_cli_request()) {
echo "Access Denied";
return;
}
Thanks in advance for any help at all. I think i just need help with what should go in the config file, but then again I may have gone down completely the wrong path altogether!
UPDATE:
So far I've got as far as this:
I believe i need to run the application from the commandline like this http://ellislab.com/codeigniter/user-guide/general/cli.html
so my command would be php index.php process_mail
So what I actually need is help with running this command evey 5 minutes. This is what I have so far:
container_commands:
send_mail:
command: php index.php process_mail
leader_only: true
But what I don't understand is how I get this to run every 5 minutes, rather than just when the instance is set up. Do I need to create a cron job file on instance creation, with the php command in it instead?
Update 2:
To anyone else with the same problem, i got this sorted in the end like this:
an ebextensions file that looks like this: (.ebextensions/mail_queue.config)
container_commands:
01_send_mail:
command: "cat .ebextensions/process_mail.txt > /etc/cron.d/process_mail && chmod 644 /etc/cron.d/process_mail"
leader_only: true
a file called process_mail.txt in the same folder that looks like this:
# The newline at the end of this file is extremely important. Cron won't run without it.
*/5 * * * * root /usr/bin/php /var/app/current/index.php process_mail > /dev/null
So, every 5 minutes it runs via the cmd line the codeigniter main index file, passing in the controller name.
thanks to this: https://stackoverflow.com/a/15233848/2604392
I would set up the cron job to talk to the url, then store result in a MySQL database. Then regular PHP or any other app can connect to MySQL and access the data. That's the suggested way to connect to Twitter since a few months, so you can find info on how to do this floowing search for Twitter connectivity.
Hope this helps
By the way, while writing an email generating PHP script, I noticed that I have to slow down the pace of email sending to avoid being flagged as spammer. I added a delay of 2 seconds between emails and it did the job. My database was only 2500 so no big deal (except taking care of changing the PHP_MAXEXECUTION time variable)...
I have an MVC architecture and since I already have an action that would be extra useful if automatically called every hour or so, I wondered if there's a way to set it up as a cron job?
Don't know how periodic web page request is related to mvc, but you can achieve this by adding following line to crontab (1 hour period):
0 0/1 * * * wget <web_page_url>
Which is translated to: use wget command to request <web_page_url> every hour at zero minutes.
You can use
curl http://example.com
Or if the language you're using has a CLI client like PHP you could just run the script like
php /var/www/example.com/index.php
Edit:
For an MCV app it's probably easiest to use curl