Jue's Blog

Feb 26, 2010

Using Gmail to send / receive emails on a different account (super complicated version)

UPDATE 3/8/2010: There is actually now a “Refresh POP” feature in Google Labs, which mostly removes the need for this hack. But check it out anyway, if you want to see some potentially useful tricks with Gmail/cron.

I love Gmail. It is simple, and I can do things with it that would usually take 10 to 10,000 times longer on anything else–Thunderbird, Outlook, other webmail systems, passenger pigeons, and corporate groupware (in order of decreasing efficiency).

I love Gmail so much that I use it to send, receive, and organize emails for 5 different addresses. Gmail makes this easy to set up. However, due to workplace policies, the simplest method–forwarding my work email straight to Gmail–isn’t allowed. Not to be deterred (but mostly because I’d rather die than use Novell Groupwise) I searched for workarounds to let me use Gmail anyway.

The result, as you can see, is an esoteric hack that probably nobody else will ever need in its exact, complete form. But until someone invents a time machine to transport all of the world’s corporate IT departments out of the 90′s, you might find some of these tricks useful.

PROBLEM: You want to use gmail to send/receive work emails

i.e. you want to login to “yourname@gmail.com” but use it to manage all emails from “yourname@work.com” and never deal with the clunky interface at work.com.

SIMPLEST/TYPICAL SOLUTION: Add yourname@work.com to your Gmail account.

This will allow you to use Gmail to send emails that are labeled as from yourname@work.com. Also, the same instructions above show you how to set up Gmail to send from your work’s SMTP servers. This makes the emails more official (and less likely to be blocked by overzealous spam filters), because not only do the emails say they are from yourname@work.com but behind the scenes, they will also legitimately show up as being from work.com’s email servers.

Then, to receive the work.com emails in Gmail, contact administrator of work.com (or find an option somewhere) to forward the email for yourname to yourname@gmail.com.

But life wouldn’t be interesting if it stopped here, right?

SIMPLEST SOLUTION DOESN’T WORK BECAUSE:

1. The emails servers for work.com block any emails whose originating server isn’t work.com.
This is a problem if you want to use Gmail to send emails as yourname@work.com to a recipient such as coworker@work.com, but easily fixed by making Gmail send emails using your work’s outgoing mail servers, UNLESS…

2. Your work blocks its SMTP servers, i.e. doesn’t let you use outside clients to send email from its servers. Then you’re shit out of luck. Fortunately, you only really ever need SMTP sending if your recipient actively blocks “spoofed” emails–ones that have a From: field with a different domain than the originating domain of the email. This is rare. If your work blocks these, just use an alternate account (like your gmail) to contact your coworkers. The oddity of your address will be a daily reminder to them of the error of their ways (or of your own stubbornness).

3. Your work doesn’t forward emails, and your administrator is unwilling to help you out, BUT your work email server supports POP access. Use POP3 fetching from Gmail (elaborated on below). No POP3? Quit your job. Seriously. And find a time machine so you can travel into the 21st century and join the rest of us. And bring your IT department with you.

4. You are lazy. Then how did you make it this far reading this?

3. POP3 fetching from Gmail: trials and tribulations

The solution I hit upon for getting my work emails through Gmail was to fetch the mail using Gmail’s Mail Fetcher. This is different from setting up mail forwarding because Gmail is pulling your mail from your work servers, rather than your work servers pushing them to Gmail. Unfortunately, the latter is still far superior, because it happens instantaneously when an email is received, whereas Gmail (and any other mail client) can only poll the server for new mail at some interval, causing delays.

In fact, the delays can get pretty bad–I can miss an entire morning of work emails because Gmail forgets to check the POP3 servers for mail for 6 hours. This is due to the algorithm Gmail uses to optimize polling frequency–if your work email goes a long time without receiving mail, Gmail will start checking it less and less frequently to save resources. This is a problem for me, in a newsroom environment, because I tend to get lots of time-sensitive emails in bursts throughout the day.

The solution? Set up something to automatically send email on a regular basis, say every 10 minutes, to yourname@work.com, so Gmail’s fetcher keeps the polling time low (in this case, between 5-10 minutes). Lifehacker and TINYenormous suggest using Automator to do this, but this only works on Macs. Here’s a more classic solution, using good old cron, the Linux task-scheduling tool. Unfortunately I can’t think of a simple way to do this in Windows, but perhaps more knowledgeable Windows hackers can suggest something.

I’m also going to assume that anybody who wants to use Gmail this bad is probably also geeky enough–like me–to have access to a webserver. Here are the steps:

1. Set up a script to send the email. I prefer this simple php script, reproduced below:

<?php
$to = "yourname@work.com";
$subject = "Sent by cron";
$body = "Hello world!";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>

Save this in your home folder, e.g. as /home/yourname/scripts/email.php

Make sure to enable execution on it, by typing this at the command prompt: chmod 755 email.php

2. Set up cron to run your script every 10 minutes (or 5, or whatever). Do this by making a file called cron.txt anywhere that is convenient, containing the line:


*/10 * * * * /home/yourname/scripts/email.php >/dev/null 2>&1

Change the “*/10″ to “*/5″ if you want it to run every 5 minutes, etc. Note the addition of >/dev/null 2>&1: this just suppresses the email that cron usually generates whenever it completes a job. You don’t need to be receiving TWO emails every 10 minutes.

After you save this file, set it as your crontab (i.e. the file that tells cron what to do) by typing:


crontab cron.txt

Now wait a little, and you should start getting this email every 10 minutes (provided the script itself isn’t having trouble sending mail). This tutorial on cron and this more detailed documentation can explain what these steps all mean, and tell you how to customize it more.

3. Set up Gmail to filter out emails from your cron job, so you can go back to productively going through work emails. Since you’re going to be getting the same email every 10 minutes from now on, you really need to automatically get rid of them or your inbox will be swamped. Fortunately Gmail makes this easy–just set up a filter and have it recognize the subject line (or sender) of every one of those automatic emails, and delete those emails as soon as they’re fetched. Instead of deleting them, you can also archive them immediately and assign them to a label, so you can at least verify that they’re coming through.

There, now your Gmail will fetch emails at least as often as your cron job sends out emails, and you have been able to stick it to the man by not using your company’s awful groupware. Hooray!

Now get back to work.

Comments

  1. I HATE HATE HATE the new version of Gmail. Every stinking time I have to send a mail I get hit with a damn quiz!

    I want the old Gmail version back with out the quiz!
    When you are on a deadline, who has time to add, subtract, multiply or divide just to SEND an email? I may deactivate this account if I cant get back to regular sending options.

    If I want a quiz to test my brain, I can pull out my DS & play Brain Games!

    September 21, 2010 @ 9:47 am
  2. Nice post.
    The problem that I have when I forward my emails from name@work.com to name@gmail.com is that whenever I click reply to such email the To field has my name@work.com email address instead of the person who sent it to me. Anyway to get around that?

    October 19, 2010 @ 4:17 pm