iPhone and Linux

Thursday, May 14, 2009

iPhone: How to save email attachments with jailbreak and Linux



The iPhone cannot save email attachments (except photos) and this is stupid. But I've figured out a way to save attachments to your phone. It requires a jailbreak and only works for Linux users. Sorry windows/mac folks, the theory should work for you too but I don't use either and can't help you.

It may seem like a lot of steps but it's pretty simple. Basically, I ssh into a PC (my home computer) and tell it to get the mail from Gmail and extract the attachment. Then I use scp to copy it to the phone. A couple simple scripts automate the process. In the end, it may take 30 seconds to actually get the file saved to your phone.

I tested this several times today from work and from a friends house and it works pretty well. Hope it helps you too.

This is setup for gmail, but any mail using POP3 should work.

Things you need:

from Cydia:

Bourne-Again SHell
MobileTerminal
ssh

On your Linux computer:

A way to get email on the Linux command line. I'm using Getmail because it's simple, but if you already use a program like fetchmail, that'll work too.

metamail - this decodes email attachments and should be on most systems already.


Step 1 - Log into gmail and make sure POP is enabled in your settings.

2- pick a temporary directory on your computer to download mail. I'm using a hidden temp directory called ".tmp" in this how-to.

3 - Create 3 directories in your temp directory called cur, new, and tmp. The "new" directory is where the action takes place but Getmail expects the others to be there and won't run without them.

4 - Set up getmail. Go to your home directory and creat a hidden directory named .getmail. In that directory, create a file called getmail.gmail. Open that file and add the following:


[retriever]
type = SimplePOP3SSLRetriever
server = pop.gmail.com
username = YOUR_LOGIN_NAME@gmail.com
password = YOUR_PASSWORD

[destination]
type = Maildir
path = ~/.tmp/

[options]
verbose = 2
message_log = ~/.getmail/gmail.

5 - Change YOUR_LOGIN_NAME@gmail.com and YOUR_PASSWORD to your gmail login info and change "path = ~/.tmp/" to whatever directory you want to use from step 2.

6 - Make a script to download email and extract the attachment. Call it whatever you like, I called mine "detach."

#! /bin/sh

# get the mail
getmail -r '~/.getmail/getmail.gmail'

# move into the mail directory
cd ~/.tmp/new

# extract the attachment
# I use a wildcard because the
# name of the files are impossibly long.
metamail *

# delete the mail.
rm *.localhost

8 - Change the "~/.tmp/new" directory in that script to whatever you picked in step 2. make the script executable by typing "chmod 775 /path/to/script" and move it into a directory in your path such as /usr/local/bin


On the iPhone:

Step 9 - Choose a directory on your phone to save files to. I'll use ~/detached as an example.

10 - Create a script to scp the file from your computer to that directory, I called the script "idetach". This can be done by opening MobileTerminal and typing the following commands:

mkdir ~/detached
su (then enter your root password for the iPhone)
echo 'scp YOUR_LINUX_NAME@YOUR_LINUX_IP:~/.tmp/new/* ~/detached' > /bin/idetach
chmod 775 /bin/idetach

Change "~/detached" to whatever directory you want to save the attchments on the phone
Change YOUR_LINUX_NAME@YOUR_LINUX_IP to the correct thing
Change "/.tmp/new/" to whatever directory your mail is in on the Linux box
Change /bin/idetach to whatever you want to call the script.

You're done.

How to use:
Getmail only downloads new messages so when you get an email with an attachment, you need to mark it as unread so it appears new again. Then ssh to your PC with MobileTerminal. Once you log in, just type "detach" (or whatever you named the script in step 6). You should see it download the message. Metamail will ask you what to do with the file. Just type a "2". (There seems to be no way around this step. You can run metamail without any user imput but it dumps the file into /tmp where there are probably dozens of other files. I'm still looking for a way to skip it though)

Then type exit to log out of the ssh session and once the connection is closed, type idetach (or whatever you named the script in step 10). Voila! The attachment will be in the directory you chose in step 9.

What if you don't have a static IP address on your home computer?

Use this script. It requires you to set up sendmail and to have lynx and nail installed, which should be on your Linux box already. It checks your IP address and checks a text file called "ip" to see if the address has changed. If it's changed, it will email you the new IP address along with the date and time. Set it as a cron job to run every 15 minutes or hour or whatever.

#! /bin/sh
date > date
ip=`lynx -dump checkip.dyndns.org | awk '{ print $4 }' | tr -d "\n\n"`
if
cat ip | grep $ip
then echo "IP hasn't changed. Not sending notification"
else
echo "Sending IP change notification"
nail -s $ip YOUR_EMAIL_ADDRESS < date
fi
echo -n `/bin/ip` > ip

Blog Archive