 {"id":34,"date":"2007-03-28T23:47:12","date_gmt":"2007-03-29T04:47:12","guid":{"rendered":"http:\/\/www.networkjack.info\/blog\/archives\/34"},"modified":"2007-03-29T10:53:42","modified_gmt":"2007-03-29T15:53:42","slug":"new-feature-last-message-received","status":"publish","type":"post","link":"http:\/\/www.networkjack.info\/blog\/2007\/03\/28\/new-feature-last-message-received\/","title":{"rendered":"New Mail Server Feature &#8211; Last Message Received"},"content":{"rendered":"<p>Today I added a new datum for the Users table for our mail server: Last Message Received<br \/>\nWhat prompted me to add this was I was trying to prune down the over 150 accounts we have in the macserve.net domain and I had no idea which email addresses were actually in use or when they last received an email. I needed a quick reference that would not necessitate a trip to the Recent Mail table.<br \/>\nSo I added two new columns to the Users table: last msg recvd and last msg sent. For now I&#8217;m only dealing with the former as I haven&#8217;t implemented tracking of sent mail yet.<\/p>\n<p>The big thing was figuring out a quick and easy way of getting the most recent datetime for a user from the Recent Mail table and updating that in the Users table.<\/p>\n<p>I started writing a separate script for this, but realized it would be just fine to drop this process into our daily database maintenance script. I had started to write some PHP code that would loop through the Recent Mail table for entries at most a week old and figure out the most recent message and then make a call to update that record in the Users table.<\/p>\n<p>In the course of creating a temporary table to go back to the archive of recent mail we keep, I realized I could simply use an SQL temporary table to hold that data (duh) and then simply run a joined update from that temp table into the Users table. It turned out to be a simple 3 statement SQL process like so:<br \/>\nCREATE TEMPORARY TABLE most_recent_email<br \/>\nSELECT recipient_id,MAX(recent_msgs.msg_when) as last_msg_when<br \/>\nFROM recent_msgs<br \/>\nWHERE (msg_when >= &#8216;$oneWeekAgo&#8217;) AND (recent_msgs.recipient_id > 0)<br \/>\nGROUP BY recent_msgs.recipient_id;<\/p>\n<p>UPDATE site_users,most_recent_mail<br \/>\nSET last_msg_rcvd = last_msg_when<br \/>\nWHERE site_users.user_id = most_recent_email.recipient_id;<\/p>\n<p>DROP TABLE most_recent_email;<\/p>\n<p>Nice and simple and I let the database do all the work for me. I like it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I added a new datum for the Users table for our mail server: Last Message Received What prompted me to add this was I&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"http:\/\/www.networkjack.info\/blog\/2007\/03\/28\/new-feature-last-message-received\/\">Continue reading<span class=\"screen-reader-text\">New Mail Server Feature &#8211; Last Message Received<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,15],"tags":[],"class_list":["post-34","post","type-post","status-publish","format-standard","hentry","category-database","category-mail-server","entry"],"_links":{"self":[{"href":"http:\/\/www.networkjack.info\/blog\/wp-json\/wp\/v2\/posts\/34"}],"collection":[{"href":"http:\/\/www.networkjack.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.networkjack.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.networkjack.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.networkjack.info\/blog\/wp-json\/wp\/v2\/comments?post=34"}],"version-history":[{"count":0,"href":"http:\/\/www.networkjack.info\/blog\/wp-json\/wp\/v2\/posts\/34\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.networkjack.info\/blog\/wp-json\/wp\/v2\/media?parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.networkjack.info\/blog\/wp-json\/wp\/v2\/categories?post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.networkjack.info\/blog\/wp-json\/wp\/v2\/tags?post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}