Skip to content

Instantly share code, notes, and snippets.

@xionluhnis
Last active December 12, 2015 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xionluhnis/4712075 to your computer and use it in GitHub Desktop.
Save xionluhnis/4712075 to your computer and use it in GitHub Desktop.
Fetching mail from pop3 via bash. Accept any pop3 command, augmented with a "last" command.
#!/bin/bash
# Wed Jun 16 16:04:19 EDT 2004
# NAME: p1
# Copyright 2004, Chris F.A. Johnson
# Released under the terms of the GNU General Public License
# Modified by Alexandre Kaspar <xion.luhnis(at)gmail(dot)com>
# Using https://gist.github.com/jjarmoc/1571540
CR=$'\r' ## carriage return; for removal of
SERV=mysmtp.com
USER=myusername
PASS=mypassword
## connect to POP3 server on local machine, port 110
exec 3<>/dev/tcp/$SERV/110
## get response from server
read ok line <&3
## check that it succeeded
[ "${ok%$CR}" != "+OK" ] && exit 5
## send user name, get response and check that it succeeded
echo user "$USER" >&3
read ok line <&3
[ "${ok%$CR}" != "+OK" ] && exit 5
## send password, get response and check that it succeeded
echo pass "$PASS" >&3
read ok line <&3
[ "${ok%$CR}" != "+OK" ] && exit 5
## get number of messages
echo stat >&3
read ok num x <&3
## display number of messages
echo $num messages
## commands
# @see https://gist.github.com/jjarmoc/1571540
alias qpd='perl -MMIME::QuotedPrint -pe '\''$_=MIME::QuotedPrint::decode($_);'\'''
shopt -s expand_aliases
if [[ $# -ge 1 ]]; then
# special "last" command
command="$@"
if [ "$command" = "last" ]; then
command="retr $num"
fi
# do the command request now
echo "$command" >&3
# check result
read ok line <&3
[ "${ok%$CR}" != "+OK" ] && echo "$ok $line" && exit 5
while read line <&3;
do
# break condition
[ "$line" = ".$CR" ] && break;
# display content line
echo $line | qpd
done
fi
## close connection
echo quit >&3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment