Thursday, March 8, 2012

Command line email with attachment

The problem: How to send an email with attachment using CLI?

Solution: Use mutt.
I am assuming that you have a SMTP server from your ISP that you can use. If you don't know what is a SMTP server then this is definitely the case. Typically the smtp server's host name is smtp.youisp.net, so if you are a comcast customer your smtp server is smtp.comcast.net

To use this smtp server you have to add an entry in ~/.muttrc file. Edit this file so that it contains something like:

set smtp_url="smtp://smtp.comcast.net:587/"
set smtp_pass="yourpassword"

Then execute mutt like this:
echo "All the text that I want in Body of the message" | mutt -a -s "Picture $name" -- youemail@yourdomain.com



For example the line below send all the pictures as attachment to my email:

for name in `ls -1 *.JPG` ; 
do 
echo "Here is the picture $name" | mutt -a $name -s "Picture $name" -- to_email@domain.com; 
echo send $name; 
done


Thats it! 

Note: 
If you need to change the from address in the email then set EMAIL environment variable to desired email address


Many ISPs are not supporting port 25 anymore and also requiring user name and password. Please see for doing all that:
http://nixtricks.wordpress.com/2010/05/05/mutt-configure-mutt-to-receive-email-via-imap-and-send-via-smtp/

Thursday, March 1, 2012

Seraching the text data on clipboard

Sometimes I need to use grep command on the certain text that I am looking at in an editor. Most of them do not have good grepping facility and even if they have there is not way to feed the results to some other process for further processing.
I ended up using a tool from nirsoft called nircmd to dump the clipboard contents in a text file and then use grep from cygwin. Here is a small 3 line script that I put in my bin folder along with the nircmd tool.

$ cat /cygdrive/c/cygwin/bin/clipGrep.sh
#!/bin/bash
MY_TMP_FILE=/cygdrive/c/tmp/UIUHUTGGUYGUhhjshjhasj_
rm $MY_TMP_FILE
nircmd.exe clipboard addfile `cygpath -w $MY_TMP_FILE`
cat $MY_TMP_FILE | grep $*
Now I can take any text on clip board and just execute this to filter what I want want. For example I selected all the text on Yahoo finance and took on clipboard (Ctrl-C) then searched for word "sell"

$ clipGrep.sh  -i sell
    Sell in March and Go Away?Matt Nesto

May not be a bad advice!