Sunday, May 13, 2012

Mutt tutorial part 4 - html mails, address book and other useful features

How mutt can handle mails in html format ? Takling about html mail handling is talking about handling any type of Multipurpose Internet Mail Extensions. Mutt supports handling for all MIME types in one place for all programs. This place is .mailcap file. Googling a little bit I found below mailcap file configuration (kudos to Bart Nagel).
text/html; pandoc -f html -t markdown; copiousoutput; description=HTML Text; test=type pandoc >/dev/null
text/html; lynx -stdin -dump -force_html -width 70; copiousoutput; description=HTML Text; test=type lynx >/dev/null
text/html; w3m -dump -T text/html -cols 70; copiousoutput; description=HTML Text; test=type w3m >/dev/null
text/html; html2text -width 70; copiousoutput; description=HTML Text; test=type html2text >/dev/null
Of course we need to install all applications to make mailcap work correct:
sudo apt-get install pandoc lynx w3m html2text
To bring address book functionality to mutt we need abook application:
sudo apt-get install abook
Also few new line in $HOME/.muttrc will be needed:
# add alias file for addresses
set alias_file=~/.mutt/alias
source ~/.mutt/alias
# configure addressbook
set query_command= "abook --mutt-query '%s'"
macro index,pager A "<pipe-message>abook --add-email-quiet<return>" "add the sender address to abook"
Adding new address simply create entry in alias file. During adding new alias abook asks about alias name, e-mail address, personal name and confirmation for given data. Some this fields could be filled automatically by interaction between abook and mutt. Of course file $HOME/.mutt/alias have to exist before running mutt:
touch $HOME/.mutt/alias
To access saved aliases simply click <Tab> button in cc, to or bcc filed.
There is few more options that I found useful. To find it please go to my workspace scripts at github. Also if you have any issues with the configuration or comments please let me know by commenting below the post.

Mutt tutorial part 3 - sidebar, URLs in e-mails

Information in this post came from lunar linux page. Kudos to its author. In this post I want to discuss two topics: sidebar and how to open urls form inside mutt. Sidebar is mutt feature delivered in mutt-patched package in Debian. It cause to split standard mutt window in two parts. In first we can find list of folders defined in the $HOME/.muttrc file, second window is a known old window with the list of posts. Mutt window with sidebar looks like on the picture below:
To use side bar we need to install mutt-patched packed in Debian:
sudo apt-get install mutt-patched
To make side bar more accessible I use default settings from lunar linux page. Add below lines to $HOME/.muttrc:
set sidebar_width       = 30
set sidebar_visible     = yes
color sidebar_new yellow default
bind index CP sidebar-prev
bind index CN sidebar-next
bind index CO sidebar-open
bind pager CP sidebar-prev
bind pager CN sidebar-next
bind pager CO sidebar-open
First line sets width of side bar it depends on how long are your folder names. Second line makes sidebar by default visible. Third makes folders with new messages yellow. Other lines create shortcuts for navigating sidebar. Note that C is not Ctrl but uppercase 'c' key. Second topic I want to discuss is how to open urls from inside e-mails. To do this we can use tip from mutt site. As it said we need urlview application:
sudo apt-get install urlview
To correctly configure this tool you need to create $HOME/.urlview file. So:
vim $HOME/.urlview
In this file we define two things. First will be regular expression which match urls and second will be command line to run when regexp was matched. File looks like below:
REGEXP (((https?|ftp|gopher)://|(mailto|file|news):)[^’ <>"]+|(www|web|w3).[-a-z0-9.]+)[^’ .,;<>":]
COMMAND chromium %s
Chromuim is my browser of choice but you can use firefox, lynx or anything you want.

Mutt tutorial part 2 - secure login

This is second post of mutt tutorial series. As in the previous post for below information I need to thank Kousik for posts about gpg and using it with mutt. But probably the most important to simplify this method was Fau comment here. So going to the point of secure login for mutt we need gpg. First of all we need to install it by simply:
sudo apt-get install gpg
After that we generate our keys by:
gpg --gen-key
I choose all default answers. So first is key type: 'RSA and RSA'. Second - keysize: 2048. Third - time for key expiration: '0 = key does not expire'. After that you need to identify your key with some data. This data will be used to find your key so IMHO it should be short and simple but meaningful. At the end of this process you will be asked about pass phrase, which will be used to decrypt files encrypted with generated key. When you end with key generation you can encrypt file with passwords. Best way to do this is to write some script that will be sourced by mutt after decryption. For storing passwords I create directory in my $HOME:
mkdir $HOME/.passwd
Inside this directory I create text file with the script, which look like below:
vim $HOME/.passwd/mutt.txt
set my_isp1 = "password1"
set my_isp2 = "password2"
set my_isp3 = "password3"
This script of course mean to set value of variable name my_isp{1,2,3} to some password string. Remember to use 'my_' prefix because this is the way that user variables should be defined in mutt scripts. After writing this file we need to encrypt it.
gpg -e -o mutt.gpg mutt.txt
Now we should delete txt file. To use our newly created encrypted password script we need to add some lines to $HOME/.muttrc. So:
vim $HOME/.muttrc
Line that we need before sourcing encrypted scripts is declaration of variables in the script:
set my_isp1 = ""
set my_isp2 = ""
set my_isp3 = ""
After this line we can source and decrypt out file with the passwords:
source "gpg --textmode -d ~/.passwd/mutt.gpg |"
At the end wee need to replace all out plain text passwords (smtp_pass and imap_pass variables) with variables defined in out encrypted file. This settings will cause that mutt during start will run gpg to ask about password to decrypt password script file. In the next post I will discuss mutt with sidebar and how to open html files from inside mutt.

Mutt tutorial part 1 - setup IMAP account

Mutt is one of those programs that make people call you a linux geek, nerd or a snob. This is because using TUI or command line tools in world of fancy GUI for most people is wierd. What's so great about mutt? I probably still have not found much of its advantages, but at first glance we can notice a few things. First, it keeps Unix convention of small programs for specific task "Make each program to one thing well" or KISS. This means that mutt is only MUA, which is used to retrieve e-mails and for other tasks you need to use another application. However, due to the philosophy most applications are well suited to each other and usually everything works good. So you can easily combine it with Vim as the mail editor, abook as a address book, urlview as a browser launcher for html and graphics elements and so on. Third, it has support for IMAP which gives very nice usage model for most of e-mail account providers. Fourth, is used by such notables as Greg Kroah-Hartman and as kernel documentation suggest by other kernel developers. So lets start to discover mutt. Below I will discuss some basic features that until now (few days of using mutt) I found useful.
  1. Big kudos to Shinobu for this post it helps me a lot. So first of all we need support for multiple accounts. In my case I have 4 accounts. Three of them got working IMAP access. 4th provider screw up something and access to IMAP server doesn't work so I need to workaround this with one of the Gmail features. At the beginning we create $HOME/.muttrc file:
    vim $HOME/.muttrc
    IMAP account configuration for looks like that:
    # unset important variables
    account-hook . "unset imap_user; unset imap_pass"
    account-hook        "imaps://<account_name>@<imap_server_address>/" "\
        set imap_user   = <e-mail_address> \
            imap_pass   = <e-mail_password>"
    
    # Setup for <e-mail_address>:
    set folder          = imaps://<account_name>@<imap_server_address>/
    # setup needed folders
    mailboxes           = +INBOX =<folder_name>
    set spoolfile       = +INBOX
    folder-hook         imaps://<account_name>@<imap_server_address>/ "\ 
        set folder      = imaps://<account_name>@<imap_server_address>/ \
            spoolfile   = +INBOX \
            postponed   = +[Gmail]/Drafts \
            record      = +[Gmail]/'Sent Mail' \
            from        = '<your_name> <e-mail_address> ' \
            realname    = '<real_name>' \
            smtp_url    = smtps://<account_name>@<smpt_server_address> \
            smtp_pass   = <e-mail_password>"
    
    <account_name> - for foo.bar@gmail.com it will be foo.bar <imap_server_address> - this information you can get from your e-mail provider help pages or from the settings of web e-mail client, for Gmail it is imap.gmail.com
    <e-mail_address> - your e-mail address
    <e-mail_password> - your e-mail password, later we will discuss how to store this more secure than plain text :)
    <folder_name> - any folder (for gmail account also filters) you have on you IMAP account, so for gmail account it could be Drafts, Starred, Important or others.
    <your_name> - your real name or nick anything you want to show in from field
    <real_name> - could be the same as <your_name>
    <smpt_server_address> - your SMTP server address, for gmail users it will be smtp.gmail.com
  2. If your e-mail provider have only pop3 access and you have gmail account you can use one of gmail account features to make your pop3 account visible as a IMAP folder. To do this got to Settings -> Accounts and Import and in the section "Check mail from other accounts" add your POP3 account. After that make sure to label your mails from POP3 account. Try to not use '@' in the label name because this cause problems during mutt configuration. If you set label for your POP3 account check if your label in Label tab have "Show in IMAP" marked, if yes everything was set correctly. To use this label in mutt simply add another <folder_name> to mailboxes line.