Difference between revisions of "OfflineIMAP"
(→Gmail configuration) |
Jasonwryan (talk | contribs) (Changed the warning to a note: less inflammatory and more accurately reflecting user experience) |
||
(14 intermediate revisions by 10 users not shown) | |||
Line 1: | Line 1: | ||
− | [[Category: | + | [[Category:Email Client]] |
− | [http://offlineimap.org/ OfflineIMAP] is | + | [http://offlineimap.org/ OfflineIMAP] is a Python utility to sync mail from IMAP servers. It does not work with the POP3 protocol or mbox, and is usually paired with a MUA such as [[Mutt]]. |
− | |||
− | |||
− | |||
==Installing== | ==Installing== | ||
Install the {{Pkg|offlineimap}} package with [[pacman]]: | Install the {{Pkg|offlineimap}} package with [[pacman]]: | ||
# pacman -S offlineimap | # pacman -S offlineimap | ||
+ | {{Note|Some users have reported sporadic issues with OfflineIMAP exhibiting random crashes and hangups requiring [[#Sporadic_Crashes|intermittent application restarts]]. Users looking for an alternative application for two-way IMAP synchronization, are advised to check [[Isync]], written in C.}} | ||
==Configuring== | ==Configuring== | ||
− | Offlineimap is distributed with two default configuration files, which are both located in {{ic|/usr/share/offlineimap/}}. {{ic|offlineimap.conf}} contains every setting and is thorougly documented. Alternatively, {{ic|offlineimap.conf.minimal}} | + | Offlineimap is distributed with two default configuration files, which are both located in {{ic|/usr/share/offlineimap/}}. {{ic|offlineimap.conf}} contains every setting and is thorougly documented. Alternatively, {{ic|offlineimap.conf.minimal}} is not commented and only contains a small number of settings (see: [[#Minimal|Minimal]]). |
Copy one of the default configuration files to {{ic|~/.offlineimaprc}}. | Copy one of the default configuration files to {{ic|~/.offlineimaprc}}. | ||
Line 81: | Line 79: | ||
# send empty data to the server to hold the connection open. A value of 60 | # send empty data to the server to hold the connection open. A value of 60 | ||
# attempts to hold the connection for a minute between syncs (both quick and | # attempts to hold the connection for a minute between syncs (both quick and | ||
− | # autorefresh) | + | # autorefresh).This setting has no effect if autorefresh and holdconnectionopen |
+ | # are not both set. | ||
keepalive = 60 | keepalive = 60 | ||
+ | # OfflineIMAP normally closes IMAP server connections between refreshes if | ||
+ | # the global option autorefresh is specified. If you wish it to keep the | ||
+ | # connection open, set this to true. This setting has no effect if autorefresh | ||
+ | is not set. | ||
+ | holdconnectionopen = yes | ||
+ | </nowiki> | ||
+ | }} | ||
+ | |||
+ | ====systemd Service==== | ||
+ | Writing a systemd service for offlineimap is trivial, just be sure to run it as the correct user by replacing the {{ic|User=UserToRunAs}} with your user. (e.g. {{ic|User=wgiokas}}) | ||
+ | {{hc|offlineimap-user.service|2= | ||
+ | <nowiki> | ||
+ | [Unit] | ||
+ | Description=Start offlineimap as a daemon | ||
+ | Requires=network.target | ||
+ | After=network.target | ||
+ | |||
+ | [Service] | ||
+ | User=UserToRunAs | ||
+ | ExecStart=/usr/bin/offlineimap | ||
+ | |||
+ | [Install] | ||
+ | WantedBy=multi-user.target | ||
</nowiki> | </nowiki> | ||
}} | }} | ||
Line 94: | Line 116: | ||
[general] | [general] | ||
# This will suppress anything but errors | # This will suppress anything but errors | ||
− | ui = | + | ui = quiet |
</nowiki> | </nowiki> | ||
}} | }} | ||
Line 143: | Line 165: | ||
$ crontab -e | $ crontab -e | ||
The line should specify the interpreter for correct {{Ic|pgrep -xf}} results: | The line should specify the interpreter for correct {{Ic|pgrep -xf}} results: | ||
− | */5 * * * * exec /usr/local/bin/start_daemon -n19 -c2 -p7 python2 /usr/bin/offlineimap | + | */5 * * * * exec /usr/local/bin/start_daemon -n19 -c2 -p7 python2 /usr/bin/offlineimap |
− | + | <span id="Sporadic_Crashes">The wrapper script is needed because offlineimap has the tendency to sporadically crash, even more so when facing connection problems.</span> | |
− | |||
− | The wrapper script is needed because offlineimap has the tendency to sporadically crash, even more so when facing connection problems. | ||
===Automatic mutt mailbox generation=== | ===Automatic mutt mailbox generation=== | ||
Line 180: | Line 200: | ||
folderfilter = lambda foldername: foldername not in '[Gmail]/All Mail' | folderfilter = lambda foldername: foldername not in '[Gmail]/All Mail' | ||
# Necessary as of OfflineIMAP 6.5.4 | # Necessary as of OfflineIMAP 6.5.4 | ||
− | + | sslcacertfile = /etc/ssl/certs/ca-certificates.crt | |
</nowiki> | </nowiki> | ||
}} | }} | ||
− | Note | + | {{Note|If you have Gmail set to another language, the folder names may appear translated too, e.g. "verzonden_berichten" instead of "sent_mail".}} |
+ | |||
+ | {{Note|After version 6.3.5, offlineimap also creates remote folders to match your local ones. Thus you may need a nametrans rule for your local repository too that reverses the effects of this nametrans rule.}} | ||
− | Note | + | {{Note|As of 1 October 2012 gmail SSL certificate fingerprint is not always the same. This prevents from using {{ic|cert_fingerprint}} and makes the {{ic|sslcacertfile}} way a better solution for the SSL verification (see [[OfflineIMAP#SSL fingerprint does not match]]).}} |
===Not having to enter the password all the time=== | ===Not having to enter the password all the time=== | ||
Line 327: | Line 349: | ||
* For working out the correct inverse mapping. the output of {{Ic|offlineimap --info}} should help. | * For working out the correct inverse mapping. the output of {{Ic|offlineimap --info}} should help. | ||
− | * After updating the mapping, it | + | * After updating the mapping, it may be necessary to remove all of the folders under {{Ic|$HOME/.offlineimap/}} for the affected accounts. |
===SSL fingerprint does not match=== | ===SSL fingerprint does not match=== | ||
ERROR: Server SSL fingerprint 'keykeykey' for hostname 'example.com' does not match configured fingerprint. Please verify and set 'cert_fingerprint' accordingly if not set yet. | ERROR: Server SSL fingerprint 'keykeykey' for hostname 'example.com' does not match configured fingerprint. Please verify and set 'cert_fingerprint' accordingly if not set yet. | ||
− | + | To solve this, add to {{ic|~/.offlineimaprc}} (in the same section as {{ic|1=ssl = yes}}) one of the following: | |
+ | * either add {{ic|cert_fingerprint}}, with the certificate fingerprint of the remote server. This checks whether the remote server certificate matches the given fingerprint. | ||
cert_fingerprint = keykeykey | cert_fingerprint = keykeykey | ||
+ | * or add {{ic|sslcacertfile}} with the path to the system CA certificates file. Needs {{Pkg|ca-certificates}} installed. This validates the remote ssl certificate chain against the Certification Authorities in that file. | ||
+ | sslcacertfile = /etc/ssl/certs/ca-certificates.crt | ||
==External links== | ==External links== |
Revision as of 04:07, 3 January 2013
OfflineIMAP is a Python utility to sync mail from IMAP servers. It does not work with the POP3 protocol or mbox, and is usually paired with a MUA such as Mutt.
Installing
Install the offlineimap package with pacman:
# pacman -S offlineimap
Configuring
Offlineimap is distributed with two default configuration files, which are both located in /usr/share/offlineimap/
. offlineimap.conf
contains every setting and is thorougly documented. Alternatively, offlineimap.conf.minimal
is not commented and only contains a small number of settings (see: Minimal).
Copy one of the default configuration files to ~/.offlineimaprc
.
Minimal
The following file is a commented version of offlineimap.conf.minimal
.
~/.offlineimaprc
[general] # List of accounts to be synced, separated by a comma. accounts = main [Account main] # Identifier for the local repository; e.g. the maildir to be synced via IMAP. localrepository = main-local # Identifier for the remote repository; i.e. the actual IMAP, usually non-local. remoterepository = main-remote # Status cache. Default is plain, which eventually becomes huge and slow. status_backend = sqlite [Repository main-local] # Currently, offlineimap only supports maildir and IMAP for local repositories. type = Maildir # Where should the mail be placed? localfolders = ~/Maildir [Repository main-remote] # Remote repos can be IMAP or Gmail, the latter being a preconfigured IMAP. type = IMAP remotehost = host.domain.tld remoteuser = username
Usage
Before running offlineimap, create any parent directories that were allocated to local repositories:
$ mkdir ~/Maildir
Now, run the program:
$ offlineimap
Mail accounts will now be synced. If anything goes wrong, take a closer look at the error messages. OfflineIMAP is usually very verbose about problems; partly because the developers didn't bother with taking away tracebacks from the final product.
Miscellaneous
Various settings and improvements
Running offlineimap in the background
Most other mail transfer agents assume that the user will be using the tool as a daemon by making the program sync periodically by default. In offlineimap, there are a few settings that control backgrounded tasks.
Confusingly, they are spread thin all-over the configuration file:
~/.offlineimaprc
# In the general section [general] # Controls how many accounts may be synced simultaneously maxsyncaccounts = 1 # In the account identifier [Account main] # Minutes between syncs autorefresh = 5 # Number of quick-syncs between autorefreshes. Quick-syncs do not update if the # only changes were to IMAP flags quick = 10 # In the remote repository identifier [Repository main-remote] # Instead of closing the connection once a sync is complete, offlineimap will # send empty data to the server to hold the connection open. A value of 60 # attempts to hold the connection for a minute between syncs (both quick and # autorefresh).This setting has no effect if autorefresh and holdconnectionopen # are not both set. keepalive = 60 # OfflineIMAP normally closes IMAP server connections between refreshes if # the global option autorefresh is specified. If you wish it to keep the # connection open, set this to true. This setting has no effect if autorefresh is not set. holdconnectionopen = yes
systemd Service
Writing a systemd service for offlineimap is trivial, just be sure to run it as the correct user by replacing the User=UserToRunAs
with your user. (e.g. User=wgiokas
)
offlineimap-user.service
[Unit] Description=Start offlineimap as a daemon Requires=network.target After=network.target [Service] User=UserToRunAs ExecStart=/usr/bin/offlineimap [Install] WantedBy=multi-user.target
cron job
1. Configure background jobs as shown in #Running offlineimap in the background.
2. Use a log-friendly user interface:
~/.offlineimaprc
[general] # This will suppress anything but errors ui = quiet
3. Write a wrapper script for daemonizing programs, or use the one shown below:
/usr/local/bin/start_daemon
#!/bin/sh set -efu ionice_class= ionice_priority= nice= while getopts c:p:n: f; do case $f in c) ionice_class=$OPTARG;; p) ionice_priority=$OPTARG;; n) nice=$OPTARG;; *) exit 2;; esac done shift $((OPTIND - 1)) cmd=$* io= if pgrep -u "$(id -u)" -xf -- "$cmd" >/dev/null 2>&1; then exit 0 fi if type ionice >/dev/null 2>&1; then [ -n "$ionice_class" ] && { io=1; cmd="-c $ionice_class $cmd"; } [ -n "$ionice_priority" ] && { io=1; cmd="-n $ionice_priority $cmd"; } [ -n "$io" ] && cmd="ionice $cmd" fi if type nice >/dev/null 2>&1; then [ -n "$nice" ] && cmd="nice -n $nice $cmd" fi exec $cmd
4. Finally, add a crontab entry:
$ crontab -e
The line should specify the interpreter for correct pgrep -xf
results:
*/5 * * * * exec /usr/local/bin/start_daemon -n19 -c2 -p7 python2 /usr/bin/offlineimap
The wrapper script is needed because offlineimap has the tendency to sporadically crash, even more so when facing connection problems.
Automatic mutt mailbox generation
Mutt cannot be simply pointed to an IMAP or maildir directory and be expected to guess which subdirectories happen to be the mailboxes, yet offlineimap can generate a muttrc fragment containing the mailboxes that it syncs.
~/.offlineimaprc
[mbnames] enabled = yes filename = ~/.mutt/mailboxes header = "mailboxes " peritem = "+%(accountname)s/%(foldername)s" sep = " " footer = "\n"
Then add source ~/.mutt/mailboxes
to ~/.mutt/muttrc
.
Gmail configuration
This remote repository is configured specifically for Gmail support, substituting folder names in uppercase for lowercase, among other small additions. Keep in mind that this configuration does not sync the All Mail folder, since it is usually unnecessary and skipping it prevents bandwidth costs:
~/.offlineimaprc
[Repository gmail-remote] type = Gmail remoteuser = user@gmail.com remotepass = password nametrans = lambda foldername: re.sub ('^\[gmail\]', 'bak', re.sub ('sent_mail', 'sent', re.sub ('starred', 'flagged', re.sub (' ', '_', foldername.lower())))) folderfilter = lambda foldername: foldername not in '[Gmail]/All Mail' # Necessary as of OfflineIMAP 6.5.4 sslcacertfile = /etc/ssl/certs/ca-certificates.crt
cert_fingerprint
and makes the sslcacertfile
way a better solution for the SSL verification (see OfflineIMAP#SSL fingerprint does not match).Not having to enter the password all the time
.netrc
Add the following lines to your ~/.netrc
:
machine hostname.tld login [your username] password [your password]
Don't forget to give the file appropriate rights like 600 or 700:
chmod 600 ~/.netrc
Gnome Keyring
http://www.clasohm.com/blog/one-entry?entry_id=90957 gives an example of how to use gnome-keyring to store the password.
python-keyring
There's a general solution that should work for any keyring. Install python-keyring from AUR, then change your ~/.offlineimaprc to say something like:
[general] pythonfile = /home/user/offlineimap.py … [Repository RemoteEmail] remoteuser = username@host.net remotepasseval = keyring.get_password("offlineimap","username@host.net") …
and somewhere in ~/offlineimap.py add import keyring
. Now all you have to do is set your password, like so:
$ python2 >>> import keyring >>> keyring.set_password("offlineimap","username@host.net", "MYPASSWORD")'
and it'll grab the password from your (kwallet/gnome-) keyring instead of having to keep it in plaintext or enter it each time.
Emacs EasyPG
See http://www.emacswiki.org/emacs/OfflineIMAP#toc2
Kerberos authentication
Install python2-kerberos from AUR and do not specify remotepass in your .offlineimaprc. OfflineImap figure out the reset all if have a valid Kerberos TGT. If you have 'maxconnections', it will fail for some connection. Comment 'maxconnections' out will solve this problem.
Troubleshooting
Overriding UI and autorefresh settings
For the sake of troubleshooting, it is sometimes convenient to launch offlineimap with a more verbose UI, no background syncs and perhaps even a debug level:
$ offlineimap [ -o ] [ -d <debug_type> ] [ -u <ui> ]
- -o
- Disable autorefresh, keepalive, etc.
- -d <debug_type>
- Where <debug_type> is one of
imap
,maildir
orthread
. Debugging imap and maildir are, by far, the most useful.
- -u <ui>
- Where <ui> is one of
CURSES.BLINKENLIGHTS
,TTY.TTYUI
,NONINTERACTIVE.BASIC
,NONINTERACTIVE.QUIET
orMACHINE.MACHINEUI
. TTY.TTYUI is sufficient for debugging purposes.
blinkenlights
, ttyui
, basic
, quiet
or machineui
.Curses interface (Curses.Blinkenlights) locks terminal
Because of a bug in Python's ncurses package (http://bugs.python.org/issue7567) the Curses interface of OfflineIMAP breaks the terminal on exit. While it appears to irreparably lock the terminal, in reality it only prevents commands from being displayed. The bug has been fixed in Python's SVN for all versions 2.6 up to 3.2 but the current package in the repositories (Python 2.6.5) is still buggy.
In order to solve the issue:
- either append
reset
to OfflineIMAP's launch command:
$ offlineimap; reset
- or change the
ui
field in~/.offlineimaprc
to select a fully functional one:
ui = TTY.TTYUI
- or as quick workaround you can just use the following command to skip the reset() function in Curses.py which causes the problem
# sed -i '125i\ \ \ \ \ \ \ \ return' /usr/lib/python2.6/site-packages/offlineimap/ui/Curses.py
netrc authentication
There are some bugs in the current offlineimap which makes it impossible to read the authentication data from ~/.netrc
if there are multiple Accounts per remote machine. ( see Mail Archive ) But they are fixed in the GIT package offlineimap-gitAUR (note that is AUR package is flagged as out of date; see the current GitHub external link below).
Using the package you can collect all passwords in ~/.netrc
. And do not forget to set it's access rights:
chmod 600 ~/.netrc
An example netrc file would be
~/.netrc
machine mail.myhost.de login mr_smith password secret
socket.ssl deprecation warnings
Depending on the currently installed python version, running offlineimap throws this warning:
DeprecationWarning: socket.ssl() is deprecated. Use ssl.wrap_socket() instead.
This can be particularly annoying when offlineimap's output is being logged or mailed through cron.
To fix the problem, apply this upstream patch or install offlineimap-gitAUR:
--- offlineimap/imaplibutil.py.orig 2009-08-12 01:24:23.000000000 -0430 +++ offlineimap/imaplibutil.py 2010-06-07 11:17:37.849038683 -0430 @@ -23,9 +23,11 @@ # Import the symbols we need that aren't exported by default from imaplib import IMAP4_PORT, IMAP4_SSL_PORT, InternalDate, Mon2num -# ssl is new in python 2.6 -if (sys.version_info[0] == 2 and sys.version_info[1] >= 6) or sys.version_info[0] >= 3: +try: import ssl + ssl_wrap = ssl.wrap_socket +except ImportError: + ssl_wrap = socket.ssl class IMAP4_Tunnel(IMAP4): """IMAP4 client class over a tunnel @@ -169,7 +171,7 @@ if last_error != 0: # FIXME raise socket.error(last_error) - self.sslobj = socket.ssl(self.sock, self.keyfile, self.certfile) + self.sslobj = ssl_wrap(self.sock, self.keyfile, self.certfile) self.sslobj = sslwrapper(self.sslobj) mustquote = re.compile(r"[^\w!#$%&'+,.:;<=>?^`|~-]")
The diff is relative to the root buildir and it can be applied by using ABS.
Folder could not be created
In version 6.5.3, offlineimap gained the ability to create folders in the remote repository, as described here.
This can lead to errors of the following form when using nametrans
on the remote repository:
ERROR: Creating folder bar on repository foo-remote Folder 'bar'[foo-remote] could not be created. Server responded: ('NO', ['[ALREADYEXISTS] Duplicate folder name bar (Failure)'])
The solution is to provide an inverse nametrans
lambda for the local repository, e.g.
~/.offlineimaprc
[Repository foo-local] nametrans = lambda foldername: foldername.replace('bar', 'BAR') [Repository foo-remote] nametrans = lambda foldername: foldername.replace('BAR', 'bar')
- For working out the correct inverse mapping. the output of
offlineimap --info
should help. - After updating the mapping, it may be necessary to remove all of the folders under
$HOME/.offlineimap/
for the affected accounts.
SSL fingerprint does not match
ERROR: Server SSL fingerprint 'keykeykey' for hostname 'example.com' does not match configured fingerprint. Please verify and set 'cert_fingerprint' accordingly if not set yet.
To solve this, add to ~/.offlineimaprc
(in the same section as ssl = yes
) one of the following:
- either add
cert_fingerprint
, with the certificate fingerprint of the remote server. This checks whether the remote server certificate matches the given fingerprint.
cert_fingerprint = keykeykey
- or add
sslcacertfile
with the path to the system CA certificates file. Needs ca-certificates installed. This validates the remote ssl certificate chain against the Certification Authorities in that file.
sslcacertfile = /etc/ssl/certs/ca-certificates.crt
External links
- Official OfflineIMAP mailing list
- Gnus, Dovecot, OfflineIMAP, search: a HOWTO
- This setup worked for me, only difference being I had to add
mail_location = maildir:~/Maildir
to/etc/dovecot/dovecot.conf
. Also, I used the Gmail configuration above. --Unhammer 09:24, 22 October 2010 (EDT)
- This setup worked for me, only difference being I had to add
- Mutt + Gmail + Offlineimap
- An outline of brisbin's simple gmail/mutt setup using cron to keep offlineimap syncing.
- Current OfflineIMAP maintainer's fork on GitHub
- Note that a strict build of this on current Arch will fail due to python references unless they are replaced with python2