I want to write some scripting for my server to read and parse emails in myemailaccount@walnutel.net to, for example, adjust the iptables firewall whitelist or blacklist. As I expand home automation, I will have more uses for this capability.

Do I even need an email client? I wonder if merely having an MTA on my server can put incoming email somewhere in the file system where a script could then read and parse it. I do have postfix installed which is giving me email sending capability (and giving me the MTA, I believe).

The email account the system would use for this is one provided by the neighborhood ISP (Walnut Communications). Currently, I neither use that email account for anything nor use my servers to read email for anything else. A side question I have along that line is 'Why am I even able to to send email without it getting authenticated through the Walnutel.net email account to give it the credentials or credibility that I thought the Internet's emailing infrastructure would demand?'

If I could just understand these two things, I could make a lot more progress. Note that the server is headless, CLI, and unattended, so I have no use for browsing an email inbox at/through it.

Edit 12/25/15: I have learned that the email methodology I seek is called "bash sockets". Unclear to me yet in my research is whether I'll for sure need to authenticate with my local email account credentials in order to send email reliably to my non-local gmail account, and whether I'll be required to use SSL (TLS, etc). If required to use SSL/TLS to communicate with email servers, I fear I'll be VERY hard-pressed to find bash scripting examples of that.


Do I even need an email client?

Yes, retrieving e-mail from a distant mail server is by definition a mail client's job. It does not need to be a full-fledged desktop program like Thunderbird however, fetchmail should do the job for you.

Why am I even able to to send email without it getting authenticated through the Walnutel.net email account to give it the credentials or credibility that I thought the Internet's emailing infrastructure would demand?

For the same reason you are able to write any address on the top-left corner of an envelope before putting it in the box without proving that you really live there: certifying the sender's identity is not a goal of the system.

  • Thnx. Re:"certifying the sender's identity", I just remember from 2001 when I was working in system support that, due to excessive and rising spam emailing, efforts by various activist Internet groups were underway to restrict email traffic to certified and trusted email agents. I guess nothing much came of that... – kenneth558 Dec 19 '15 at 20:44
  • @kenneth558 There are some efforts to add some degree of confidence in the sender address, but they are not implemented by everybody. – fkraiem Dec 20 '15 at 2:43
  • I chanced upon a possible way to both send and receive email from CLI commands without postfix or anything! It is by sending SMTP and ESMTP commands out to the walnutel.net MX server with exec 5<>/dev/tcp/. I'm working on it now. And it does use walnutel.net authentication with the ESMPT AUTH command like I expected, as well as base64 encoding. Only thing is, I wonder if I'll have to talk encrypted to port 587 instead of clear to port 25. If it works, it'll be exactly what I've been wanting for a long time! – kenneth558 Dec 20 '15 at 6:00
  • ...I should say, port 465 encrypted or port 587... – kenneth558 Dec 20 '15 at 6:18

I succeeded in fetching and parsing of emails with bash-socket-scripting without any email client at all. This was made easy by virtue of my ISP (Walnut Communications) not requiring SSL to download emails from their mail server. Had I needed to do this inside Cox Communications network who verbally stated that they require SSL to do the same, it would have been more difficult because the necessary openssl s_client -connect pop.cox.net:995 -CApath /etc/ssl/certs command seems to be interactive only.

Here is bash script for logging into a local ISP-provided email account not requiring SSL. BE AWARE THE PASSWORD IS PLAIN TEXT IN THIS EXAMPLE SCRIPT, so you may want to add a password hiding measure to these commands.

# interactive use enabled by [ ! -z $PS1 ] && echo... 
exec 5<>/dev/tcp/pop3.walnutel.net/110
read -t 2 -u 5 HELO;[ ! -z $PS1 ] && echo "$HELO"
echo -e "user email_account_name_here\r" >&5 #prepend account name with "user "
read -t 2 -u 5 sendyourpassword;[ ! -z $PS1 ] && echo $sendyourpassword
echo -e "pass password_here\r" >&5 #prepend password with "pass "
read -t 2 -u 5  maildroplockedandready;[ ! -z $PS1 ] && echo -e $maildroplockedandready
echo -e "list\r" >&5
echo "" > ~/mailcontent;numofemails=-1;echo "" > ~/email_fetch_parse.log
while read  -t 2 -u 5 emailtitle; do
    [ ! -z $PS1 ] && echo "$emailtitle"
    [ $(( ${#emailtitle} )) -lt 3 ] && break
    if [ $(( ${#emailtitle} )) -lt 20 ]; then
         #add to array of emailtitlnums
         numofemails=$(( $numofemails + 1 ))
         emailtitlenums[ $((numofemails)) ]=${emailtitle% *}
if [ $((numofemails)) -gt -1 ]; then
    for i in `seq 0 $((numofemails))`;do
         echo -e "RETR ${emailtitlenums[i]}\r" >&5
          while read  -t 2 -u 5 mailcontentline;do
             [ ! -z $PS1 ] && echo "$mailcontentline"
             mailcontentline="${mailcontentline:: -1} " #strip the linefeed and put space there instead
              if [ -z "$boundary" ];then [ $(echo "$mailcontentline"|grep -e "^Content-Type: "|grep -c "; boundary=") -gt 0 ] && boundary="$(echo "$mailcontentline"|grep -e "^Content-Type: " -e "; boundary="|/usr/bin/awk -F= '{print $2}')"
                  echo "$(grep -e "search string 1" -e "search string 2,etc"  <(echo "$mailcontentline"))" >> ~/mailcontent #save lines of interest
                  [ $(bc <<<"${#mailcontentline} - ${#boundary}") -eq 4 ] && [ "${mailcontentline:2: -3}" == "${boundary:: -1}" ] && (for i in `seq 1 4`; do read -t 2 -u 5 mailcontentline;done) && break #
    for i in `seq 0 $((numofemails))`;do # to delete emails immediately
         echo -e "dele ${emailtitlenums[i]}\r" >&5
         while read -t 2 -u 5 deleteresponse;do [ ! -z $PS1 ] && echo "$deleteresponse";[ $(( ${#deleteresponse} )) -lt 1 ] && break;done

# parse ~/mailcontent here if you want. It now contains lines of interest from all emails read

       [ ! -z $PS1 ] && echo "Inbox is empty" # >> ~/email_fetch_parse.log
echo -e "QUIT\r" >&5
exec 5>&-

On the second question [I] asked, I've come to learn that my cell phone provider texting service stopped accepting direct emails as of late from my [unauthenticating] home system. That means my system can no longer send me texts of home security events until I get scripting completed to authenticate to one of my authenticating email accounts that is still able to send me texts. So, with shell scripting, I eliminated the need for an email client to fetch email, and once I get scripting working for outgoing emails, my security system will no longer need any third-party mailing tools, whether mailutils, procmail, sendmail, or etc. The advantage for me is that I won't have config settings unknown to me and difficult/inconvenient to determine their appropriate values needed in my application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.