Archive

Posts Tagged ‘PHP’

Monitoring incoming and outgoing email with PHP

December 8th, 2009

More than once I’ve tried to be more productive during a working day by limiting the times of the day that I check and respond to emails. I first came across this idea in the book The 4 hour work week and again in Do it Tomorrow.

I’ve tried working with having only certain times of the day when I check my email. It works for a day or so but I usually fail to keep going for any number of reasons such as meetings, phone calls (because I didn’t respond to an email) or even my own habit of opening up my mail without even thinking.

I wondered if the idea was right but that I was trying to enforce it at the wrong times. Rather than trying multiple different time periods I wrote a PHP script to monitor my email usage, watching the times that people email me and watching the times I email other people.

Using a graph of the hours of the day, I was hoping I might see one or more times a day that there was a natural lul in email activity that I could use to work away without checking emails. Looking at the graph there are little dips near lunch and after 5 but no obvious spots during a working day where I could have an uninterrupted work period.

emails_received_november_09

In that graph it shows some emails coming in at odd hours. These are my own automated emails which email me when someone has reached this blog after searching Google. It shows me the search term used. Other than this, I keep other automated emails to another account.

1,365 emails coming in during November works out at about 45 per day or 68 per working day.

emails_sent_in_november_09

427 emails being sent by me in November works out at 14 per day or 21 per working day. I thought it would have been more.

Some day I must look at the amount of text in these emails to work out the average time it takes for me to read and write the emails.

So starting in December I decided to start work a little earlier and see if I could get a few things done before the emails and phone calls get going.

My new habit is getting up at 6am and getting straight to work from home. Luckily my coffee machine has a timer so I can have a pot of coffee ready and piping hot when I wander into the kitchen in the dark.

For a week or so now I’ve been working 6am to 10am without checking my email or answering the phone during that time. I have set my email client (Thunderbird) not to check mails on start-up because I often need to open it to refer to older mails and its calendar without getting new mail.

It’s just less than 4 hours of incredibly productive time. I then head in to the office in The Rubicon where I work the rest of my day where emails, phone calls and meetings can happen.

The PHP script for monitoring the emails

To monitor my outgoing emails, I edited the settings in ‘Copies and Folders’ in the Thunderbird account settings. There is an option there to automatically Bcc an outgoing email.

To monitor my incoming emails, I added an email address to a ‘Forward list’ in the email address settings in the Blacknight control panel.

I then wrote a PHP script to open a connection to both incoming and outgoing email addresses every half hour using Cron. The script opens a POP3 connection, reads in the headers of each of the incoming and outgoing emails to a mysql database and then deletes them.

The information stored to the database are the To field, From field, a copy of the headers and the date/time.

Having copies of the incoming and outgoing emails in this way allows me to interact with my usual email without the scheduled script from missing any emails to record information from.

Another PHP script reads in the incoming email information from the database for each month and creates a bar chart using the Google Chart API.

Here are the main points from the PHP script which reads in the email headers from the server.

Open a connection to the server: (Where SERVER_ADDRESS is your email host address)

$mbox = imap_open(”{SERVER_ADDRESS:110/pop3}”, “$username”, “$password”);

Collect the headers in the inbox currently:

$headers = imap_headers($mbox);

Loop through the headers:

foreach ($headers as $val) {

Get a specific email header

$email_headers=addslashes(imap_fetchheader($mbox, $count));

Pull out some specific information, the ‘To’ field using a regular expression:

preg_match($pattern, $email_headers, $matches);
$email_to = str_replace(”To: “,”", $matches[0]);
$pattern = “/to: (.+)+/i”;

Get the ‘from’ field:

$pattern = “/from: (.+)+/i”;
preg_match($pattern, $email_headers, $matches);
$email_from = addslashes(str_replace(”From: “,”", $matches[0]));

Pull out any other information needed such as the date received or the email client used and then save to a database table.

Label the email to be deleted:

imap_delete($mbox, $count);

End the loop through the headers:

}

Delete the marked emails and close the connection:

imap_expunge($mbox);
imap_close($mbox);

Send me a comment below if you would like me to email you the entire script to connect PHP to a POP3 mailbox, save the headers and display a bar chart using the Google Chart API. The code is a bit much to copy here.

Which links are users pressing to leave a page?

November 10th, 2009

Murrion Software HomepageA few months ago I wrote a script to monitor the links on a webpage that users are clicking on to leave the page.

The script is a combination of; CSS to label the links to be tracked, Javascript to trigger the code when the link is clicked and finally PHP to store the time, current page, destination page, browser agent and IP address of the user.

The links use a Class within the link to label the links to monitor  and doesn’t change the link itself, retaining any referral address.

I wanted to test this out over time and give it an easy page to work with rather than putting it on to the blog straight away to monitor the links there.

I put the script on my own Murrion Software homepage which is a simple screen showing a logo and 3 links. This links go to this blog, a ‘tools’ page containing other free tools and scripts and also to a ‘contact’ page.

Here are the (rounded)  percentages of clicks each button received from August to October inclusive.

‘Tools’ link :  41%
‘Blog’ link : 31%
‘Contact’ link : 17%

10% left the page without clicking any link.

It’s not quite a heat-map but it’s interesting to see. I’ve swapped around the buttons now to see if their position has any influence on clicks over the next 3 months. I imagine there will be little or no difference.

Author: Gordon Murray Categories: development Tags: , ,

murmurs 04/09/2009

September 4th, 2009

Amazing high speed robotic hand. (YouTube video)

Some research from Red Cardinal about Malware Stats for Irish Web Hosting Companies.

RefactormyCode.com Gget your code improved  or help others improve their code.

Server2Go, Create a server which runs off a CDROM or USB stick, very handy and very free too.

Use FormIgniter to create forms for use with CodeIgniter. It outputs the neccessary model, view and controll code. A great time saver.

murmurs 14/08/2009

August 14th, 2009

DesignFellow have released CodeIgniter quick reference cheat sheet version 2.0.

A blog post on Why PHP frameworks matter.

Test your app in any browser from the web. Very useful service.

17 Awesome Web Developer Cheat Sheets, some great cheat sheets, PHP, Ruby, mySQL and more.

Earlier in the week, I wrote about differences in the referral address from Google and Google ‘Caffeine’, Googles new search engine algorithm. On the Blackdog SEO blog, Paul compares the two search tools and has created an excellent tool to compare the results from Google and Google Caffeine.

murmurs 31/07/2009

July 31st, 2009

Code in your browser with PHPanywhere. Theres also a techcrunch article about it.

If you think a website might be infected with the Gumblar virus? Try out this free tool and read this blog post on how to remove it.

Is this the video of the future? 360 degree video.

DesignFellow are giving away a free CodeIgniter Cheat sheet. I have mine printed out and stuck on the wall.

An excellent tutorial from Nettuts, develop an app in CodeIgniter and then developing the exact same app using Ruby on Rails. From CodeIgniter to Ruby on Rails: A Conversion

Author: Gordon Murray Categories: murmurs Tags: , ,

Email marketing stats with CodeIgniter

July 8th, 2009
CodeIgniter - Open source PHP web application ...
Image by guspim via Flickr

At the start of June, I began collecting email newsletters together to collect information from them. My main interest was in finding answers such as the most common day and time for sending email newsletters, how many links and images do businesses include in their newsletters?

So far, I have collected over 300 emails newsletters from over 50 sources with an average of 9 new email newsletters coming in per day. A summary of all the information collected is on the email newsletter information page.

At first, I wrote a simple PHP script to gather together the information and show it on the page. After receiving a few dozen large newsletters the script began timing out as it took too long for the information to be worked out and displayed. I rewrote the system for collecting the newsletter information and displaying it using CodeIgniter, a PHP framework using the MVC approach.

I used CodeIgniters Active Record class in a Model to retrieve and calculate the summary information to display in a View. Its performance is amazing, the same information that originally took too long to display, resulting in a time-out now displays in a second or more, without Caching. CodeIgniter has fantastic performance.

If you are developing any apps using PHP, I highly recommend taking a look at CodeIgniter, there is a fantastic User guide, Wiki and forum.

Reblog this post [with Zemanta]