Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit b3e471a

Browse files
committed
Fixed email problems.
1 parent 58a9f7d commit b3e471a

File tree

4 files changed

+134
-67
lines changed

4 files changed

+134
-67
lines changed

.DS_Store

0 Bytes
Binary file not shown.

classes/class.email.php

+120-62
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,131 @@
33
* Class to create and send an email outlining upcoming (next 2 weeks) and past-due loads
44
*
55
* @author Jared Howland <[email protected]@gmail.com>
6-
* @version 2013-04-23
6+
* @version 2013-04-25
77
* @since 2013-04-23
88
*/
9-
9+
1010
class email {
11-
/**
12-
* Constructor. Displays information from the database
13-
*
14-
* @access public
15-
* @param null
16-
* @return boolean, string TRUE/FALSE status of emailing, string with message if no resources need to be loaded in the next 2 weeks
17-
*/
18-
public function send_email() {
19-
$resources = $this->get_resources();
20-
return $this->get_email($resources);
11+
/**
12+
* Constructor. Emails past due and upcoming records to load.
13+
*
14+
* @access public
15+
* @param null
16+
* @return boolean TRUE/FALSE status of email (sent/not sent)
17+
*/
18+
public function send() {
19+
$message = $this->get_message();
20+
if($this->send_message($message)) {
21+
return TRUE;
22+
} else {
23+
return FALSE;
24+
}
25+
}
26+
27+
/**
28+
* Emails list of resources that need to be loaded in the next 2 weeks to appropriate parties (as defined in config.php)
29+
*
30+
* @access public
31+
* @param string String containing the message to be emailed.
32+
* @return boolean TRUE/FALSE status of email (sent/not sent)
33+
*/
34+
private function send_message($message) {
35+
$to = config::NOTIFY_EMAILS;
36+
$subject = 'MARC Records to Load for ' . date('D, M d, Y');
37+
$headers = 'From: ' . config::FROM_EMAIL . "\r\n" . 'Reply-To: ' . config::FROM_EMAIL . "\r\n" . 'X-Mailer: PHP/' . phpversion();
38+
ob_start();
39+
return mail( $to, $subject, $message, $headers );
40+
}
41+
42+
/**
43+
* Create email message.
44+
*
45+
* @access public
46+
* @param null
47+
* @return string Message to be sent via email
48+
*/
49+
private function get_message() {
50+
$past_resources = $this->get_past_resources();
51+
$future_resources = $this->get_future_resources();
52+
$message = NULL;
53+
if($past_resources) {
54+
$message .= 'The following resources are past due:' . "\r\n\r\n" . $this->get_resource_list($past_resources) . "\r\n\r\n";
55+
}
56+
if($future_resources) {
57+
$message .= 'The following resources need to be loaded in the next two weeks:' . "\r\n\r\n" . $this->get_resource_list($future_resources);
2158
}
22-
23-
/**
24-
* Gets list of all resources that need to be loaded in the next 2 weeks
25-
*
26-
* @access public
27-
* @param null
28-
* @return array Array with data about resources
29-
*/
30-
private function get_resources() {
31-
$date = date('Y-m-d', strtotime('+2 weeks'));
32-
$database = new db;
33-
$db = $database->connect();
34-
$sql = 'SELECT r.resource_name, r.next_load, v.name AS vendor_name FROM records r INNER JOIN vendors v ON r.vendor_id = v.id WHERE r.next_load < :date ORDER BY r.next_load ASC, vendor_name, resource_name';
35-
$query = $db->prepare($sql);
36-
$query->bindParam(':date', $date);
37-
$query->execute();
38-
$resources = $query->fetchAll(PDO::FETCH_ASSOC);
39-
$db = null;
40-
return $resources;
59+
if(!$past_resources && !$future_resources) {
60+
$message .= 'We are all up to date. There are no resources to load at this time.';
4161
}
42-
43-
/**
44-
* Emails list of resources that need to be loaded in the next 2 weeks to appropriate parties (as defined in config.php)
45-
*
46-
* @access public
47-
* @param array Array with resources that need to be loaded in the next 2 weeks
48-
* @return boolean, string TRUE/FALSE status of emailing, string with message if no resources need to be loaded in the next 2 weeks
49-
*/
50-
private function get_email($resources) {
51-
$count = count($resources);
52-
// Only send email if MARC records need to be loaded in the next 2 weeks
53-
if($count > 0) {
54-
$resource_list = NULL;
55-
foreach($resources as $resource) {
56-
$resource_name = $resource['resource_name'];
57-
$next_load = $resource['next_load'];
58-
$vendor_name = $resource['vendor_name'];
59-
$resource_list .= $next_load . ': ' . $vendor_name . '-' . $resource_name . "\r\n";
60-
}
61-
$to = config::NOTIFY_EMAILS;
62-
$subject = 'MARC Records to Load for ' . date('D, M d, Y');
63-
$message = 'The following resources need to be loaded soon:' . "\r\n\r\n" . $resource_list;
64-
$headers = 'From: ' . config::FROM_EMAIL . "\r\n" .
65-
'Reply-To: ' . config::FROM_EMAIL . "\r\n" .
66-
'X-Mailer: PHP/' . phpversion();
67-
ob_start();
68-
$mail = mail( $to, $subject, $message, $headers );
69-
return $mail;
70-
} else {
71-
return 'Congratulations! No resources need to be loaded in the next 2 weeks.';
72-
}
62+
return $message;
63+
}
64+
65+
/**
66+
* Format array as a list (string) for email message
67+
*
68+
* @access public
69+
* @param array Array containing about resources to email
70+
* @return string List of resources from array
71+
*/
72+
private function get_resource_list($resources) {
73+
$resource_list = NULL;
74+
foreach($resources as $resource) {
75+
$resource_name = $resource['resource_name'];
76+
$next_load = $resource['next_load'];
77+
$vendor_name = $resource['vendor_name'];
78+
$resource_list .= $next_load . ': ' . $vendor_name . '-' . $resource_name . "\r\n";
79+
}
80+
return $resource_list;
81+
}
82+
83+
/**
84+
* Returns array of all resources that are past due
85+
*
86+
* @access public
87+
* @param null
88+
* @return array, boolean Array with data about resources. Returns FALSE if query returns no results.
89+
*/
90+
private function get_past_resources() {
91+
$date = date('Y-m-d');
92+
$database = new db;
93+
$db = $database->connect();
94+
$sql = 'SELECT r.resource_name, r.next_load, v.name AS vendor_name FROM records r INNER JOIN vendors v ON r.vendor_id = v.id WHERE r.next_load < :date ORDER BY r.next_load ASC, vendor_name, resource_name';
95+
$query = $db->prepare($sql);
96+
$query->bindParam(':date', $date);
97+
$query->execute();
98+
$past_resources = $query->fetchAll(PDO::FETCH_ASSOC);
99+
$db = null;
100+
if(count($past_resources) > 0) {
101+
return $past_resources;
102+
} else {
103+
return FALSE;
104+
}
105+
}
106+
107+
/**
108+
* Returns array of all resources that need to be loaded in the next 2 weeks
109+
*
110+
* @access public
111+
* @param null
112+
* @return array, boolean Array with data about resources. Returns FALSE if query returns no results.
113+
*/
114+
private function get_future_resources() {
115+
$date = date('Y-m-d');
116+
$future_date = date('Y-m-d', strtotime('+2 weeks'));
117+
$database = new db;
118+
$db = $database->connect();
119+
$sql = 'SELECT r.resource_name, r.next_load, v.name AS vendor_name FROM records r INNER JOIN vendors v ON r.vendor_id = v.id WHERE r.next_load between :date AND :future_date ORDER BY r.next_load ASC, vendor_name, resource_name';
120+
$query = $db->prepare($sql);
121+
$query->bindParam(':date', $date);
122+
$query->bindParam(':future_date', $future_date);
123+
$query->execute();
124+
$future_resources = $query->fetchAll(PDO::FETCH_ASSOC);
125+
$db = null;
126+
if(count($future_resources) > 0) {
127+
return $future_resources;
128+
} else {
129+
return FALSE;
73130
}
131+
}
74132
}
75133
?>

edit.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
$load_records = $resource[0]['load_records'];
4141
if($load_records == 'Y') {
4242
$check_yes = ' checked="yes"';
43-
$check_no = '';
43+
$check_no = '';
4444
} else {
4545
$check_yes = '';
46-
$check_no = ' checked="yes"';
46+
$check_no = ' checked="yes"';
4747
}
4848
$vendor_name = $resource[0]['vendor_name'];
4949
$html = <<<HTML

email.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,25 @@
33
* Creates and sends an email outlining upcoming (next 2 weeks) and past-due tasks
44
*
55
* @author Jared Howland <[email protected]>
6-
* @version 2013-04-23
6+
* @version 2013-04-25
77
* @since 2013-04-23
88
*
99
*/
1010

1111
require_once 'config.php';
12+
13+
// Log that this page was accessed.
1214
$date = date('r') . "\n";
1315
$fp = fopen('email_log', 'a');
14-
fwrite($fp, 'Date: ' . $date);
16+
fwrite($fp, $date);
1517
fclose($fp);
18+
19+
// Send email
1620
$email = new email;
17-
$email->send_email();
21+
$sent = $email->send();
22+
if($sent) {
23+
echo '<p>Email successfully sent.</p>';
24+
} else {
25+
echo '<p>There was a problem sending the email. Please try again.</p>';
26+
}
1827
?>

0 commit comments

Comments
 (0)