bradkenyon Posted September 10, 2008 Share Posted September 10, 2008 I am not a very effiecient coder, so I am looking to break down an almost duplicated process into one, and just call the function for three different instances. I send out two emails, one to students, and one to faculty. Both receive the following three things: today's events, upcoming events, and food items Each pull different data into each email, besides the food, that goes to both, and any events that are marked as "both" 2 = students 3 = faculty 1 = both (go to both faculty and students) food = 4 (go to both faculty and students) here is the code that sends the email. i am thinking i can just have one function, that does a bunch of checks for if there is a value in that field of the record, then include it, otherwise don't display it in the email. help would be greatly appreciated. this code calls the function to send the email to appropriate email: eventEmail('2','Student Daily Bulletin','students@domain.com'); eventEmail('3','Faculty/Staff Daily Bulletin','faculty@students.com'); this is the code that pulls the appropriate data for today's events, upcoming events, and food items: <?php require_once("../customemailer/htmlMimeMail.php"); include('../cgi-bin/newsinclude/db.php'); function eventEmail($type, $subject, $emailto) { $today = date("Y-m-d"); //query for today's event $query = "select * from calendar_items WHERE expiredate LIKE '$today %' and (event_type_id = '$type' or event_type_id = 1)"; $result=mysql_query($query); //query for future upcoming event to appear today $upcoming = "select * from calendar_items WHERE upcoming_event_featured_date LIKE '$today %' and (event_type_id = '$type' or event_type_id = 1)"; $result_upcoming=mysql_query($upcoming); //query for food items to appear today $fooditems = "select * from calendar_items WHERE expiredate LIKE '$today %' and event_type_id = '4'"; $result_fooditems=mysql_query($fooditems); print '<h1 style="color:red;">'.mysql_num_rows($result).'</h2>'; print '<h1 style="color:red;">'.mysql_num_rows($result_upcoming).'</h2>'; //if there is an event, then proceed, otherwise do not send email if(mysql_num_rows($result) > 0 || mysql_num_rows($result_upcoming) > 0 || mysql_num_rows($result_fooditems) > 0) { //checks to see if there is an event for today if(mysql_num_rows($result) > 0) { print '<h1>Today '.$type.'</h1>'; $message.="<h1 style=\"font-size:16px;\">Today's Events</h1>"; ?> <ul> <?php while($row = mysql_fetch_array($result)) { $i = 0; while($i <=0) { print '<p><strong>'.date("l F j, Y",strtotime($row['expiredate'])).'</strong> - <a href="/events/?id='.$row['id'].'">'.$row['subj'].'</a> - <small>'.$row['location'].'</small></p>'; $message.= "<p><span style=\"font-weight:bold; color:#00472D; font-size:14px;\">".$row['subj']."</span><br /> <span style=\"font-size:14px;\">".date("g:ia",strtotime($row['expiredate']))." / ".$row['location']."<br /> ".nl2br($row['body'])."</span>"; if($row['docurl']) { $message.='<br>Document Attached: <a href="http://herkimer.edu/docs/?docid='.$row['id'].'">Click to download</a>'; } $i++; print '</p>'; } } print '</ul>'; } //checks to see if there is a future event that is displayed in today's email if(mysql_num_rows($result_upcoming) > 0) { print '<h1>Events down the road...</h1>'; $message.="<h1 style=\"font-size:16px;\">Events down the road...</h1>"; while($row_upcoming = mysql_fetch_array($result_upcoming)) { $u = 0; while($u <=0) { print '<p>'.$row_upcoming['subj'].$row_upcoming['id'].'</p>'; print '<p><strong>'.date("l F j, Y",strtotime($row_upcoming['expiredate'])).'</strong> - <a href="/events/?id='.$row_upcoming['id'].'">'.$row_upcoming['subj'].'</a> - <small>'.$row_upcoming['location'].'</small></p>'; $message.= "<p><span style=\"font-weight:bold; color:#00472D; font-size:14px;\">".$row_upcoming['subj']."</span><br /> <span style=\"font-size:14px;\">".date("l F j, Y",strtotime($row_upcoming['expiredate']))." / ".$row_upcoming['location']."<br /> ".nl2br($row_upcoming['body'])."</span>"; if($row_upcoming['docurl']) { $message.='<br>Document Attached: <a href="http://herkimer.edu/docs/?docid='.$row_upcoming['id'].'">Click to download</a>'; } $u++; print '</p>'; } } } //checks to see if there is a food item to send within the email if(mysql_num_rows($result_fooditems) > 0) { print '<h1>Dining Choices</h1>'; $message.="<h1 style=\"font-size:16px;\">Dining Choices</h1>"; while($row_fooditems = mysql_fetch_array($result_fooditems)) { $f = 0; while($f <=0) { print '<p>'.$row_fooditems['subj'].$row_fooditems['id'].'</p>'; $message.= "<p><span style=\"font-weight:bold; color:#00472D; font-size:14px;\">".$row_fooditems['subj']."</span><br /> <span style=\"font-size:14px;\">".date("g:ia",strtotime($row_fooditems['expiredate']))." / ".$row_fooditems['location']."<br /> ".nl2br($row_fooditems['body'])."</span>"; if($row_fooditems['docurl']) { $message.='<br>Document Attached: <a href="http://herkimer.edu/docs/?docid='.$row_fooditems['id'].'">Click to download</a>'; } $f++; print '</p>'; } } } //if there something in result and/or result_upcoming, it will run this $mail = new htmlMimeMail(); $mail->setHTML($message); $mail->setSubject($subject); $mail->setFrom("no-reply@domain.com"); $result = $mail->send(array($emailto)); //echo $result ? 'Mail sent!' : 'Failed to send mail'; } else { print '<p>Nothing to send in the email, so it was not sent.</p>'; } } function DownloadFile($file) // $file = include path { if(file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.