Saturday, January 7, 2017

Send email with PHP and crontab on reboot in Ubuntu

Even with the best VPS services reboots can happen: maintenance, hardware upgrade, kernel upgrade. Most of the time users are warned in advance so that they can prepare for the service outage. However, it can happen that there is a reboot and you did not know about it or simply forgot. 

I just added a small php script to my server to send me an email if it was rebooted. This way I can log in ASAP and restart services that do not start automatically.

Send email via PHP

This assumes that you have php installed on your Ubuntu instance.

A simple php program to send emails can look like this:

<?php
$to = "email@address.com";
$subject = "Reboot";
$txt = '

Hello, your server has been rebooted and services have most likely stopped.
If this reboot was unscheduled please log in to restart services.

Have a nice day!
';


// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

//More headers
$headers = "From: server@server.com" . "\r\n" .
"CC: ";

mail($to,$subject,$txt,$headers);
?>

It is kind of self-explanatory, but just to explain quickly: 

to: email address of the recipient
subject: subject of the email
text: main body of the email until the final semicolon (;). Note that you can also send html-based emails. To do this, simply used the html codes such as,
$text = '<html>
<head>
</head>
<body>
This is a html <br> message!
</body>
</html>'
headers: sets the message header. You can write any email address in the "from" field.
mail($to,$subject,$txt,$headers); : sends the email with the previously defined variables above.

Create and test

  1. You can save the above in a .php file. (I prefer to have a MyScripts directory in my /home, just to keep everything organized.)
  2. Test if the program is working.
    sudo -u www-data php -f rebootMail.php
    This will call php, as the user "www-data", and launch the specified file. If your web server's default user is not www-data, adjust accordingly.

Note: If you have a web server running, you can run the above php program from your web browser. Simply copy the rebootMail.php into your /var/www (or wherever your web server's / is) and visit serveraddress.com/rebootMail.php. If you have done everything correctly, you should receive an email. Although, in this case the php program can be executed by anyone visiting it's web address.

Auto-launch program on reboot 

This can be done either via crontab or using /etc/rc.local. Let's do it via crontab.
  1. To edit existing cron scheduled tasks (no sudo needed),
    crontab -e
  2. In a new line at the end of the file add,
    @reboot /usr/bin/php /path/to/rebootMail.php
  3. Save changes and exit the nano editor,
    Ctrl + O
    Ctrl + X
Done. If all done correctly, you should now receive an email on every reboot. To make sure everything is well configured, you should reboot your server manually once and confirm that it works.

Note: If your php is not installed in /usr/bin, adjust the path accordingly.

No comments:

Post a Comment