A scheduled report by e-mail: a script, a cron job and an SMTP account

To get a summary of the site in your e-mail every morning (the day’s orders, enquiries received, new sign-ups) you need three pieces: a PHP script that fetches the figures from the database, an e-mail account the message goes out through, and a cron job that runs the script at the set time. All of it fits on a shared hosting plan.

The three pieces

Piece What it does Watch out for
PHP script Runs the query, writes the text and sends it. Keep it outside public_html. Credentials go in a separate file.
SMTP account It is how the e-mail goes out. Use a mailbox on your domain, with the connection details cPanel shows for that account.
Cron job Runs the script at the right time. The time is the server’s.

Step by step

1 Decide which figures. Three or four are enough: how many orders, how many enquiries, how many new sign-ups. A report nobody reads to the end stops being opened.
2 Create a read-only database user. In cPanel, under MySQL Databases, create a user and give it only the query permission (SELECT). If the report is stolen, nobody can change anything.
3 Write the script. It connects with that user, runs the queries and builds plain text with the date in the subject. To send, use PHPMailer with SMTP: see sending e-mail from your own code with PHPMailer.
4 Run it by hand once and confirm the e-mail arrives, and arrives in the inbox.
5 Schedule it. Once a day, at an hour when the server is quiet, such as the small hours or before you reach work. The line has the full path to PHP and to the script: see running a PHP script from a cron job.
6 Keep a log. Add >> /home/ACCOUNT/report.log 2>&1 to the line, so you know what happened if one day the e-mail does not come.

Design the report to be read in ten seconds. The most important figure in the e-mail subject, the rest in a few lines, always in the same order. Compare with yesterday or last week: a figure on its own says little, a change says a lot. If you want more detail, add a link rather than a huge table.

Do not put too much personal data in the e-mail. A report with full names, phone numbers and amounts stays in mailboxes, copies and phones. Send totals and counts, and a link to the panel where the detail is protected.

If the report lands in the junk folder, the problem is the domain and not the script: see SPF, DKIM and DMARC: why your e-mail lands in spam. If the database user or password changes, the script fails silently and the e-mail simply stops arriving. That is one more reason to notice when it is missing.

Make the script report when there is nothing: “today, zero orders”. If a day’s e-mail goes missing, you know at once the problem is the job and not the business.

Need a plan with cPanel, cron and mailboxes? See the hosting plans.

See the plans

SEE ALSO

Sending e-mail from your own code with PHPMailer

Running a PHP script from a cron job

Cron jobs: what they are for and how to create one

Hosting plans

RECOMMENDED PRODUCT

Professional e-mail on your domain

Mailboxes in your company name, no adverts, with spam filtering. from 5.940,00 Kz/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?