How to force Gmail to check your POP3 account as often as possible

NOTE: This technical article was published long time ago. It may be not up to date. Please check for the newest versions of mentioned software. Also I am not able to provide technical support anymore. Thank you for your understanding.

One, of the killer features of Gmail (in my opinion) is ability to collect e-mails from external POP3 accounts and send messages via external SMTP servers. This way, one can use Gmail as slick, web based e-mail client to virtually any account in any domain, with the benefit of IMAP access, labels, autoresponders, spam filtering, etc.

Unfortunately, Gmail uses “intelligent” algorithm to figure out, how often it should collect e-mail messages from your POP3 server. It is based on the amount of mail you get – if you receive merely few messages per day, Gmail will check the server twice every hour. In extreme situation it means 29-minute delay in delivering the message that you are waiting for – not very convenient lag, hard to accept in the 21st century. If you get your e-mails often, Gmail is going to check your account every five minutes or so.

So, how to get your account checked every few minutes (= almost no delay in delivery of your important messages)? Lure Gmail into thinking, that you get maaany important emails every few minutes. You can accomplish it by simple Python script, that you should run on your server (i.e. Raspberry Pi with Raspbian, that you can run in your home). (Click “Continue…”)

Simple Python script (you can download it from this github repository):

import smtplib, time
from email.mime.text import MIMEText

to_list = ['[email protected]']
from_email = '[email protected]'

s = smtplib.SMTP('smtp.example.com')
s.login('username', 'password')

msg = MIMEText('server status: OK')
msg['Subject'] = 'Server status '+time.ctime()
msg['From'] = from_email

for to in to_list:
    msg['To'] = to
    print msg.as_string()
    s.sendmail(from_email, [to], msg.as_string())

And then, in the crontab (crontab -e):

*/5 9-22 * * * /usr/bin/python /home/pi/scripts/email-ping.py
*/15 23,0-8 * * * /usr/bin/python /home/pi/scripts/email-ping.py

The script itself sends meaningless message (“Server status: OK” – at least you know, if your server is working) to your e-mail (the one, that is being checked by Gmail – not the Gmail address). When you configure the script to run every few minutes (as in example – every 5 minutes from 9AM to 10PM and every 15 minutes during the night), the Gmail sees, that there is new mail accumulated every time it checks the account, so it adjusts the frequency accordingly. After few hours, your account will be checked every few minutes, and your mail delivered almost instantly.

As you don’t want to have your inbox flooded with these messages, don’t forget to add filter, and send them directly to Trash.