in reply to Blank data file

This increments a tracking counter for use by another script. The problem is that the data file goes blank randomly.

You have a timing window in the code. Consider what happens if you execute   open(file, ">$mailclicks); immediately before another process obtains a lock. They now read (and will later write) a blank file.

You might want to expand the lifetime of the lock to encompas the entire process.