· 9 years ago · Dec 23, 2016, 03:34 PM
1<?php
2 $dbname = "birthdays";
3 $tablename = "birthdays";
4 $to_email = "sendto@thisperson.com";
5
6
7 $servername = "localhost";
8 $username = "root";
9 $password = "password";
10
11 $birthdates = array();
12
13 // Create connection
14 $conn = new mysqli($servername, $username, $password);
15
16 // Check connection
17 if ($conn->connect_error) {
18 die("Connection failed: " . $conn->connect_error);
19 }else{
20 $createDatabaseSQL = "CREATE DATABASE IF NOT EXISTS ".$dbname.";";
21 $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;";
22
23 mysqli_query($conn, $createDatabaseSQL);
24 mysqli_select_db($conn, $dbname);
25 mysqli_query($conn, $createTablesSQL);
26
27 if(isSet($_REQUEST["action"]) && $_REQUEST["action"] == "cron_check"){
28 $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')";
29 $result = mysqli_query($conn, $sql);
30
31 if (mysqli_num_rows($result) > 0) {
32 // output data of each row
33 while($row = mysqli_fetch_assoc($result)) {
34 $birthdates[] = array("id" => $row["id"], "name" => $row["name"], "birthdate" => $row["birthdate"], "age" => $row["age"]);
35 }
36 }
37 if(sizeOf($birthdates) > 0){
38
39 $to = $to_email;
40 $subject = 'Birthday reminder';
41 $message = '<h1>Todays birthdays:</h1>';
42 $message .= '<table border=1>';
43 $message .= '<tr>';
44 $message .= '<th>ID</th>';
45 $message .= '<th>Name</th>';
46 $message .= '<th>Age</th>';
47 $message .= '<th>Birthday</th>';
48 $message .= '</tr>';
49 if(sizeof($birthdates) !== 0){
50 foreach($birthdates as $birthdate){
51 $message .= "<tr>";
52 $message .= "<td>" . $birthdate["id"] . "</td>";
53 $message .= "<td>" . $birthdate["name"] . "</td>";
54 $message .= "<td>" . $birthdate["age"] . "</td>";
55 $message .= "<td>" . $birthdate["birthdate"] . "</td>";
56 $message .= "</tr>";
57 }
58 }
59 $message .= "</table>";
60
61 $domain = parse_url("https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
62 $domain = $domain["host"];
63 $headers = 'From: birthdayreminder@' . $domain . "\r\n" .
64 'Reply-To: webmaster@' . $domain . "\r\n" .
65 'X-Mailer: PHP/' . phpversion() . "\r\n" .
66 'Content-Type: text/html; charset=ISO-8859-1\r\n';
67
68 $result = mail($to, $subject, $message, $headers);
69 if($result){
70 echo "Reminder sent";
71 }else{
72 echo "Reminder not sent";
73 }
74 }
75
76 die();
77 }
78
79 }
80?>