Fetch mail from Gmail through Mutt

>Mutt  is a wonderful mail client ,which is commandline based. I have been using it for quite some time now.In this article I will show you how you can get mail from your gmail account.

If you don’t have it then please get it from the specific OS repository or the link provided at the begining of this article.

I am sitting on Gentoo and I got it like below:

bhaskar@bhaskar-laptop_09:46:16_Wed Dec 15:~> sudo emerge -av mutt

These are the packages that would be merged, in order:

Calculating dependencies... done!
[ebuild R ] mail-client/mutt-1.5.20-r18 USE="berkdb crypt gdbm nls ssl -debug -doc* -gnutls* -gpg* -idn -imap* -mbox* -nntp -pop* -qdbm -sasl* -sidebar -smime* -smtp* -tokyocabinet" 0 kB

Total: 1 package (1 reinstall), Size of downloads: 0 kB

Would you like to merge these packages? [Yes/No]

So I already have it in my system so there is no point getting it again.Let me check out when I got this;

bhaskar@bhaskar-laptop_09:49:46_Wed Dec 15:~> sudo genlop -t mutt
* mail-client/mutt

Wed Nov 24 11:16:02 2010 >>> mail-client/mutt-1.5.20-r18
merge time: 10 minutes and 53 seconds.

Anyway ,it deflates configuration files in /etc/mutt like below:

bhaskar@bhaskar-laptop_09:51:46_Wed Dec 15:/etc/mutt> ls
Muttrc Muttrc.dist mime.types mime.types.dist

Now What I do;read few article on the internet about gmail integration with mutt(which I will provide the link in the resource section at the bottom of this post).Now I have copied the Muttrc file to my home dir and make adjustment to that file.

My personal .Muttrc look like below:

1 # Some minimal Mutt settings, Gentoo-style. Since mutt was installed
2 # with USE=mbox, this file doesn't have any changes from the default
3 # mutt settings, making it very minimal indeed...
4 #
5 # Please don't add settings to this file to change other user
6 # preferences (such as colors), since those can be hard for a user to
7 # undo if their preference doesn't match yours! For example, it is
8 # *impossible* currently in mutt to remove color settings from objects
9 # other than the index.
10 #
12 set imap_user="unixbhaskar@gmail.com"
13 set imap_pass=""
14 set pager_context=1
15 set pager_index_lines=6 #show a mini-index in pager
16 set menu_scroll
17 set pgp_verify_sig=no #dont show pgp in pager
18 set status_on_top #put status line at top
19 set sort=threads #sort by message threads in index
20 #
21 set status_format=" %r %b %f %n Del %d Msgs %m %l %> (%P)"
22 set pager_format="%-10.10i %[!%a %b %d %R]"
23 set date_format="!%H:%M %a %d %b "
24 set index_format="%4C %Z %[%b%d] %-15.15F %s"
25 set folder_format="%2C %t %8s %d %N %f"
26 #
27 # #set sendmail="/usr/bin/nbsmtp -d isp.net -h smtp.isp.net -f yourname@isp.net"
28 #
29 set from="unixbhaskar@gmail.com" #set to your from address
30 set realname="Bhaskar Chowdhury"
31 #
32 # set record="$home/sent" #sent mail is saved here
33 set delete=yes #delete without prompting
34 set include=yes #quote msg in reply
35 set fast_reply=yes #no prompting on reply
36 set beep=no #no noise
37 set markers=no #no + on wrapped lines
38 set confirmappend=no #no prompt for save to =keep
39 set to_chars=" +TCF" #no L for mail_list
40 #
41 set folder = "imaps://imap.gmail.com:993"
42 set spoolfile = "+INBOX"
43 set postponed ="+[Gmail]/Drafts"
45 set header_cache =~/.mutt/cache/headers
46 set message_cachedir =~/.mutt/cache/bodies
47 #mailboxes =inbox
48 # mailboxes =gentoo-dev
49 # mailboxes =gentoo-announce
50 # mailboxes =gentoo
51 # mailboxes =freshmeat
52 # mailboxes =duplicates
53 # mailboxes =default
54 # mailboxes =friends
55 # mailboxes =junk
56 # mailboxes =spam
57 # mailboxes =keep
58 #
59 set smtp_url = "smtp://unixbhaskar@smtp.gmail.com:587/"
60 set smtp_pass = ""
61 save-hook .* =keep #default mbox to (s)ave mail is =keep
62 # subscribe gentoo-user gentoo-dev #subscribed to these lists
63 #
64 # bind pager h display-toggle-weed #toggle headers with h key
65 #
66 # # simulate the old url menu
67 macro index \cb |urlview\n 'call urlview to extract URLs out of a message'
68 macro pager \cb |urlview\n 'call urlview to extract URLs out of a message'
69 #
70 # #run fetchmail by hitting key of G
71 macro index G "!fetchmail -a -m 'procmail -d %T'\r"
72 macro pager G "!fetchmail -a -m 'procmail -d %T'\r"
73 #
74 # #use to edit .muttrc and then source it...no restart necessary
75 # macro generic ,sm ":source $HOME/.muttrc\r"
76 # macro generic \cj "!rxvt -bg wheat -e joe $HOME/.muttrc\r"
77 #
78 # # default list of header fields to weed out when displaying mail
79 # #ignore them all and then unignore what you want to see
80 # ignore *
81 unignore Date To From: Subject X-Mailer Organization User-Agent
82 hdr_order Date From To Subject X-Mailer User-Agent Organization
83 #
84 # ##your Mutt has to have some colors
85 # ##these are for four levels of quoted text
86 # ##they override the system settings in /etc/mutt/Muttrc
87 #
88 # #color quoted green default
89 color quoted1 magenta blue
90 # #color quoted2 yellow default
91 # #color quoted3 red default
92 # #color signature cyan cyan
93 #
94 #
95 # #this color setup is copied from /etc/mutt/Muttrc.color
96 # #comment it out if you want the default colors in /etc/mutt/Muttrc
97 # # Je vois la vie en rose :-)
98 color hdrdefault brightcyan blue
99 color header brightwhite blue "^from:"
100 color header brightwhite blue "^subject:"
101 #
102 color quoted brightgreen blue
103 color signature brightwhite blue
104 #
105 color indicator blue green
106 #
107 color error red black
108 mono error bold
109 color status black cyan
110 mono status bold
111 color tree yellow blue
112 #
113 color tilde brightmagenta blue
114 color body brightwhite blue "[-a-z_0-9.]+@[-a-z_0-9.]+"
115 mono body bold "[-a-z_0-9.]+@[-a-z_0-9.]+"
116 color body brightyellow black "^Good signature"
117 mono body bold "^Good signature"
118 color body brightwhite red "^Bad signature from.*"
119 mono body bold "^Bad signature from.*"
120 color normal white blue
121 color message green black
122 color attachment brightgreen blue
123 #
124 # # End of file...but it can go on and on and on....:)
125 #

So the next job is to invoke mutt from the commandline like below:


and here is the outcome of that..it’s connecting to gmail..

Now look at the bottom of the picture it fetching the header of those messages(it doesn’t download the whole message).Only when you select the message for viewing then only mutt download the whole message.Once it get the headers it shows off the inbox like below:

Once the the headers are fetched it display the mail boxes you have with Gmail.Then you need to choose the proper box to show off those mails reside in it.Now select the any inbox and press enter then:

Anyway you can also integrate GnuPG with it for encryption and signing.

Hope this will help.



1) http://www.andrews-corner.org/mutt.html

2) http://wiki.mutt.org/index.cgi?MuttGuide

3) http://crunchbanglinux.org/wiki/howto/howto_setup_mutt_with_gmail_imap

4) http://www.faqs.org/docs/Linux-HOWTO/Mutt-GnuPG-PGP-HOWTO.html

SASL : IMAP authentication system

>In this article I am going to take you through cyrus-sasl to built with postfix mail server.So fasten your seat belt for the ride.

SASL stand for Simple Authentication Security Layer and I will integrate it with an IMAP server built with postfix.SASL is defined in RFC-2222.SASL is a means for authenticating yourself to the server without providing your password in the clear. This can also be used to provide extended capabilities based on your authorization. In plainer words, a SASL mechanism can provide authentication only, or it can also provide integrity checking, and possibly encryption as well.

I do not issue any guarantee that this will work for you.

First and foremost thing to get the cyrus-sasl source from here or go to the website dedicated to it.

Ok, I am building it on Gentoo,so I will furnish the steps required to get it work on it.But the prime focus would be to show you how it work.

bhaskar@bhaskar-laptop_08:22:37_Sat Sep 18:~> sudo emerge -av cyrus-sasl

These are the packages that would be merged, in order:

Calculating dependencies… done!
[ebuild R ] dev-libs/cyrus-sasl-2.1.23-r1 USE=”berkdb crypt gdbm ldap pam ssl -authdaemond -java -kerberos -mysql -ntlm_unsupported_patch -postgres -sample -sqlite -srp -urandom” 0 kB

Total: 1 package (1 reinstall), Size of downloads: 0 kB

Would you like to merge these packages? [Yes/No]

I choose N or no here because I have it already in the system.But when did I have it into the system? lets find out:

bhaskar@bhaskar-laptop_08:47:55_Sat Sep 18:~> sudo genlop -t cyrus-sasl
* dev-libs/cyrus-sasl

Fri Nov 13 18:08:00 2009 >>> dev-libs/cyrus-sasl-2.1.23-r1
merge time: 2 minutes and 5 seconds.

Right! now move on. We need to add an user to operate to manage that software.So here we go:

bhaskar@bhaskar-laptop_08:49:18_Sat Sep 18:~> sudo useradd -g mail cyrus

Now you see the user like this:

bhaskar@bhaskar-laptop_08:49:18_Sat Sep 18:~> id cyrus
uid=110(cyrus) gid=12(mail) groups=12(mail)

Next we change the password of it ,the user cyrus created in the previous steps, like this:

root@bhaskar-laptop_09:02:10_Sat Sep 18:/home/bhaskar # passwd cyrus
Retype new password:
passwd: password updated successfully

Cool! You can go ahead and test the user before you start to implement other thing with it.So now little bit of placeholders need to created for it.

Creating the necessary directories

This list of instructions will set up all the directories necessary for imap.

1. mkdir /var/adm

2. touch /var/adm/imapd.log /var/adm/auth.log

3. mkdir /var/imap /var/spool/imap /var/imap/srvtab

4. chown cyrus /var/imap /var/spool/imap /var/imap/srvtab

5. chgrp mail /var/imap /var/spool/imap /var/imap/srvtab

6. chmod 750 /var/imap /var/spool/imap /var/imap/srvtab

Pretty easily understandable those commands ,if not please cut and paste it your system to see the effect of it.

Now lets working as user cyrus created earlier..so the chances will be less to intermingle with other thing:

root@bhaskar-laptop_09:12:35_Sat Sep 18:/home/bhaskar # su cyrus

cyrus@bhaskar-laptop_09:12:39_Sat Sep 18:/home/bhaskar>

Now we are going to put some entry into the syslog config file so the logger will take action on it.

bhaskar@bhaskar-laptop_09:18:02_Sat Sep 18:/etc/syslog-ng> sudo vim syslog-ng.conf

and we put into it below lines:

local6.debug /var/adm/imapd.log
auth.debug /var/adm/auth.log

Ok,one more thing to do before I jump into the cyrus thing. We need to edit the file called /etc/imapd.conf,because we are integrating with an imap server.

bhaskar@bhaskar-laptop_09:22:38_Sat Sep 18:~> sudo vim /etc/imapd.conf

Once you are inside the file please add those below line and save the file:

configdirectory: /var/imap
6 partition-default: /var/spool/imap
7 sievedir: /var/imap/sieve
9 tls_ca_path: /etc/ssl/certs
10 tls_cert_file: /etc/ssl/cyrus/server.crt
11 tls_key_file: /etc/ssl/cyrus/server.key
13 # Don’t use an everyday user as admin.
14 admins: cyrus
16 hashimapspool: yes
17 allowanonymouslogin: no
18 allowplaintext: no

So these are entry I have put in.As I have ssl cert too!.I have use the sasldb to check my password and the entry look in the file is :

# Use saslauthd if you want to use pam for imap.
27 # But be warned: login with DIGEST-MD5 or CRAM-MD5
28 # is not possible using pam.
29 sasl_pwcheck_method: saslauthd

Now we need to check the services file which reside in /etc directory to hold the information about the system services.We need to look into that file for some specific lines mentioned below:

pop3 110/tcp
imap 143/tcp
imsp 406/tcp
kpop 1109/tcp
sieve 2000/tcp


root@bhaskar-laptop_09:36:28_Sat Sep 18:/var/spool # grep pop3 /etc/services
pop3 110/tcp pop-3 # Post Office Protocol – Version 3
pop3 110/udp pop-3
pop3s 995/tcp # pop3 protocol over TLS/SSL
pop3s 995/udp


root@bhaskar-laptop_09:37:15_Sat Sep 18:/var/spool # grep imap /etc/services
imap 143/tcp imap2 # Internet Message Access Protocol
imap 143/udp imap2
imap3 220/tcp # Interactive Mail Access
imap3 220/udp
imaps 993/tcp # imap4 protocol over TLS/SSL
imaps 993/udp


root@bhaskar-laptop_09:37:22_Sat Sep 18:/var/spool # grep imsp /etc/services
imsp 406/tcp # Interactive Mail Support Protocol
imsp 406/udp


root@bhaskar-laptop_09:38:29_Sat Sep 18:/var/spool # grep kpop /etc/services
kpop 1109/tcp # Pop with Kerberos


root@bhaskar-laptop_09:39:09_Sat Sep 18:/var/spool # grep sieve /etc/services
cisco-sccp 2000/tcp sieve # Cisco SCCP
cisco-sccp 2000/udp sieve

So things are in place.Cool! looks good indeed.Now we need to modify the superserver file called inetd or in modern system called xinetd.

imap stream tcp nowait cyrus /usr/cyrus/bin/imapd imapd
pop3 stream tcp nowait cyrus /usr/cyrus/bin/pop3d pop3d

As I said before that we are going to be integrated with postifx,so need to check out this thing in /etc/postfix/master.cf for the user cyrus :

root@bhaskar-laptop_09:58:51_Sat Sep 18:/etc # grep cyrus /etc/postfix/master.cf
cyrus unix – n n – – pipe
# flags=R user=cyrus argv=/cyrus/bin/deliver -e -m ${extension} ${user}
# Also specify in main.cf: cyrus_destination_recipient_limit=1
#cyrus unix – n n – – pipe
# user=cyrus argv=/cyrus/bin/deliver -e -r ${sender} -m ${extension} ${user}

So in my system it in place but if you don’t have this thing set please do that.

Now we need to add the cyrus administrator for the monitoring and administrative works.Here is the steps:

root@bhaskar-laptop_10:12:00_Sat Sep 18:/etc # /usr/sbin/saslpasswd2 cyrus
Again (for verification):

Now it’s time for the testing the server with authentication…so here we go:

cyrus@bhaskar-laptop_10:46:00_Sat Sep 18:/root> cyradm –auth login localhost
verify error:num=18:self signed certificate
IMAP Password:

bhaskar-laptop.localdomain> ?
authenticate, login, auth authenticate to server
chdir, cd change current directory
createmailbox, create, cm create mailbox
deleteaclmailbox, deleteacl, dam remove ACLs from mailbox
deletemailbox, delete, dm delete mailbox
disconnect, disc disconnect from current server
exit, quit exit cyradm
help, ? show commands
info display mailbox/server metadata
listacl, lam, listaclmailbox list ACLs on mailbox
listmailbox, lm list mailboxes
listquota, lq list quotas on specified root
listquotaroot, lqr, lqm show quota roots and quotas for mailbox
mboxcfg, mboxconfig configure mailbox
reconstruct reconstruct mailbox (if supported)
renamemailbox, rename, renm rename (and optionally relocate) mailbox
server, servername, connect show current server or connect to server
setaclmailbox, sam, setacl set ACLs on mailbox
setinfo set server metadata
setquota, sq set quota on mailbox or resource
subscribe, sub subscribe to a mailbox
unsubscribe, unsub unsubscribe from a mailbox
version, ver display version info of current server
xfermailbox, xfer transfer (relocate) a mailbox to a different server

Creating mailbox for the specified user

bhaskar-laptop.localdomain> cm user.bhaskar
bhaskar-laptop.localdomain> lm
user.bhaskar (\HasNoChildren)

Here “lm” stands for list mailbox, which is available by the help command shown above.

Now you can do so many thing with the mail server namely create a user,set quota for their mails box to name a few.Please look at the command enlisted above to utilise it.

Hope this will help.