Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $dbname = "birthdays";
- $tablename = "birthdays";
- $to_email = "sendto@thisperson.com";
- $servername = "localhost";
- $username = "root";
- $password = "password";
- $birthdates = array();
- // Create connection
- $conn = new mysqli($servername, $username, $password);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }else{
- $createDatabaseSQL = "CREATE DATABASE IF NOT EXISTS ".$dbname.";";
- $createTablesSQL = "CREATE TABLE IF NOT EXISTS `".$dbname."`.`".$tablename."` ( `id` INT NOT NULL AUTO_INCREMENT , `name` VARCHAR(250) NOT NULL , `birthdate` TIMESTAMP NULL DEFAULT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;";
- mysqli_query($conn, $createDatabaseSQL);
- mysqli_select_db($conn, $dbname);
- mysqli_query($conn, $createTablesSQL);
- if(isSet($_REQUEST["action"]) && $_REQUEST["action"] == "cron_check"){
- $sql = "SELECT id, name, birthdate, YEAR(CURRENT_TIMESTAMP) - YEAR(birthdate) - (RIGHT(CURRENT_TIMESTAMP, 5) < RIGHT(birthdate, 5)) as age FROM `".$tablename."` WHERE DATE_FORMAT(birthdate,'%m-%d') = DATE_FORMAT(NOW(),'%m-%d')";
- $result = mysqli_query($conn, $sql);
- if (mysqli_num_rows($result) > 0) {
- // output data of each row
- while($row = mysqli_fetch_assoc($result)) {
- $birthdates[] = array("id" => $row["id"], "name" => $row["name"], "birthdate" => $row["birthdate"], "age" => $row["age"]);
- }
- }
- if(sizeOf($birthdates) > 0){
- $to = $to_email;
- $subject = 'Birthday reminder';
- $message = '<h1>Todays birthdays:</h1>';
- $message .= '<table border=1>';
- $message .= '<tr>';
- $message .= '<th>ID</th>';
- $message .= '<th>Name</th>';
- $message .= '<th>Age</th>';
- $message .= '<th>Birthday</th>';
- $message .= '</tr>';
- if(sizeof($birthdates) !== 0){
- foreach($birthdates as $birthdate){
- $message .= "<tr>";
- $message .= "<td>" . $birthdate["id"] . "</td>";
- $message .= "<td>" . $birthdate["name"] . "</td>";
- $message .= "<td>" . $birthdate["age"] . "</td>";
- $message .= "<td>" . $birthdate["birthdate"] . "</td>";
- $message .= "</tr>";
- }
- }
- $message .= "</table>";
- $domain = parse_url("https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
- $domain = $domain["host"];
- $headers = 'From: birthdayreminder@' . $domain . "\r\n" .
- 'Reply-To: webmaster@' . $domain . "\r\n" .
- 'X-Mailer: PHP/' . phpversion() . "\r\n" .
- 'Content-Type: text/html; charset=ISO-8859-1\r\n';
- $result = mail($to, $subject, $message, $headers);
- if($result){
- echo "Reminder sent";
- }else{
- echo "Reminder not sent";
- }
- }
- die();
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement