Schedule SMS with PHP and cron
This quick guide will demonstrate how to create a simple PHP script to automate weekly SMS send-outs.
What you will accomplish…
Every Saturday at 8 am an SMS is automatically sent to all gym members with a link to a schedule of the coming week’s class schedule.
What you need to follow this guide…
- A 46elks account.
- Ability to SSH into a Linux server.
Let’s get started🚀
Create a PHP file that sends SMS
This PHP file sends a preset message to a defined list of recipients. In your code editor, create a PHP file and copy the code so it looks like the example below, except with your 46elks authorization details where indicated by comments.
Parts of a cron command
It is good to have an overview of cron before we dive into our example. Below is a brief overview of cron commands and how they work. Here is a simple cron job example:
10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1
There are two main components of a cron job:
First, the timing: 10 * * * *
- here you set the minutes, hours, days, months, and weekday settings (more below).
Second, the command itself:
/usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1
The command itself in this example has three parts:
/usr/bin/php
- PHP scripts are usually are executable by themselves which is why we need to run it through the PHP parser. You can use the command which php
to check the correct path.
/www/virtual/username/cron.php
- This is just the path to the script.
> /dev/null 2>&1
- (optional) This part is handling the output of the script.
In the above example, the cron job is set to send every 10 minutes — or more specifically, every 10th minute of every hour of every day of every month, every day of the week. An asterisk is a wildcard that stands for “all”.
Here is a break down of the timing elements:
Setting up our cronjob
Below is all the information you need to create a cron job that runs every Saturday at 8 am. Once you have access to your server, you can check if any current jobs are running with the following command to list all crontabs:
crontab -l
This will list the crontab contents.
(NOTE: There may be none.)
Now, we’ll edit the crontab:
crontab -e
At this point, assuming you’ve never changed the default editor for your server, you may find yourself in the vim editor. It can be a little confusing and scary the first time you use it, so here’s what to do:
- Press ESC.
- Type “i” (for “insert”) to begin editing the file.
- Paste the cron command into the file.
- Press ESC
- Type “:wq” (for “write and quit”) to save and exit.
That’s it! 🥳 Now you can sit back and your gym members will be happy to receive their updates.
Some other potential use-cases for cron jobs
- Sending our reminders and updates to your staff or customers
- Checking the status of your servers and website and notifying your support team
- Requesting monthly feedback from your team
- Checking your database for unpaid bills and sending notices
- Sending yourself weekly SMS updates on your investments