Unixbhaskar's Blog

Wednesday, December 15, 2010

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=""
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 -h -f"
28 #
29 set from="" #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://"
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://"
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 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'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.