LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-17-2014, 10:49 AM   #1
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 132

Rep: Reputation: Disabled
Bash smtp


Hey Guys,

Many thanks to everyone that helped me out on my other thread.. I've marked that as complete and I've learned a lot on the way.. this query is in relation to the same script but I have bigger ideas...

I have downloaded an SMTP Script that was kindly written by Laas over at http://blog.laaz.org/ - he's shared this script with all and it looks great.

Problem is, it does not work for me. I've added a variable for host, and I want the host to be the domain name instead of the hostname.. and I'd like to call the script from inside other scripts with the parameters for that particular email (as details on Laas' site)

Is there something simple I'm missing?

Code:
#!/bin/bash
 
# Function to connect to SMTP and send mail to it
# returns:
# 0 - SUCCESS
# 1 - FAILURE: unable to complete session to DATA command
# 2 - FAILURE: problems after DATA command (server did not enqueue the message)
function bash_mail(){
  # Default configuration
  # Can be overridden by ARGV in order: TO, SUBJECT, MESSAGE, FROM, SERVER
  HOST=bathspa.ac.uk
  TO=${1:-'xxxxxx@bathspa.ac.uk'}
  SUBJECT=${2:-"Probleem ${HOSTNAME}"}
  MESSAGE=${3:-'Panic! Come to rescue.'}
  FROM=${4:-"root@$HOST"}
  SERVER=${5:-'mailxxxxx.bathspa.ac.uk'}
 
  # start a subshell to aquire own set of filedescriptors
  # Note! echo output must be redirected to 0, else it defaults to 1
  (
 
    # Compose all commands in an array
    # starts from 1 to be in sync with reply codes (see below)
    cmds=(
      "HELO $HOST\r\n"
      "MAIL FROM: $FROM\r\n"
      "RCPT TO: $TO\r\n"
      "DATA\r\n"
      "From: $FROM\r\nTo: $TO\r\nSubject: $SUBJECT\r\n\r\n${MESSAGE}\r\n.\r\n" 
      "QUIT\r\n"
    )
 
    # expected reply codes in order
    codes=( 220 250 250 250 354 250 )
 
    # iterate over all commands
    for (( i = 0; i < ${#cmds[*]}; i++ ))
    do
 
      # read input
      read code msg <&0
 
      # check that server replied with correct success code
      if [ $code = ${codes[$i]} ]
      then
        echo -en ${cmds[$i]} >&0
      else
        break
      fi
    done
    
    # workaround to deliver $i outside the subshell
    exit $i
  )<>/dev/tcp/$SERVER/25
  
  # recatch the $i
  i=$?
 
  # result codes based on where the loop broke off
  results=( [5]=2 [6]=0 )
  # if defined, return from results array, else return 1
  return ${results[$i]:-1}
}

Last edited by jonnybinthemix; 06-17-2014 at 10:50 AM.
 
Old 06-17-2014, 11:14 AM   #2
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 132

Original Poster
Rep: Reputation: Disabled
To be honest I think my biggest weakness here is that I really can't seem to understand the above script..

I'm seeing Telnet Commands, and I totally understand how to send mail through a mailserver by telnetting into it.. however I don't see anywhere in this script where it calls the mail server and then issues these SMTP commands.

The Server is defined in $SERVER but then where is that called?
 
Old 06-17-2014, 03:59 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
Quote:
Originally Posted by jonnybinthemix View Post
I'm seeing Telnet Commands, and I totally understand how to send mail through a mailserver by telnetting into it.. however I don't see anywhere in this script where it calls the mail server and then issues these SMTP commands.
There are no "telnet commands". The cmds array holds SMTP commands. echo -en ${cmds[$i]} >&0 sends one of them to the pipe which is connected to the server by <>/dev/tcp/$SERVER/25.

By the way, I'm not seeing this script on the page you linked to.
 
Old 06-17-2014, 04:21 PM   #4
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 132

Original Poster
Rep: Reputation: Disabled
Ahh I see, so it connects to the server using <>dev/tcp/úSERVER/25 so it's connecting to the server in $SERVER on port 25.. makes sense. But how does it connect? I guess in a similar way to telnet on port 25? Just a generic connection through port 25 to pass commands?

So as I understand, it connects to the server and then passes the commands in the array to the server, the server then issues a response code per each command which are compared against the response codes in the $CODES variable? codes=( 220 250 250 250 354 250 ) I checked and these codes compare to the response codes that an SMTP server returns when those commands are passed to it manually.

If the above it correct and my basic understanding is okay, I'm still not sure why it doesn't work for me?

I hope it means something to the experts on here (yourself very much included) that I'm not here to get work 'done' for me, I'm here to learn and absorb as much of your knowledge as your happy to share with me I am new to this but I'm really excited to learn more and I'm enjoying everything I'm doing

Thanks again for your help.
Jon

---------- Post added 06-17-14 at 03:22 PM ----------

Ooh sorry, I forgot to add... the script is from this page:http://blog.laaz.org/tech/2010/01/25/bash-smtp/
 
Old 06-17-2014, 04:44 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
Quote:
Originally Posted by jonnybinthemix View Post
Ahh I see, so it connects to the server using <>dev/tcp/úSERVER/25 so it's connecting to the server in $SERVER on port 25.. makes sense. But how does it connect? I guess in a similar way to telnet on port 25? Just a generic connection through port 25 to pass commands?
When you write to the pseudo file /dev/tcp/<server>/<port> bash opens a TCP connection and sends the data over it instead of writing to the file (and the same for reading). There's an explanation from the blog you linked to: http://blog.laaz.org/tech/2009/11/24...nd-networking/

Quote:
If the above it correct and my basic understanding is okay, I'm still not sure why it doesn't work for me?
Not sure, but it might help to print out the server's response to see what's happening:
Code:
      # read input
      read code msg <&0
      # echo the server's response to standard error
      echo "$code $msg" >&2
Quote:
I hope it means something to the experts on here (yourself very much included) that I'm not here to get work 'done' for me,
Yup, I don't bother responding to the "gimme ur codez plz" kind of threads.
 
Old 06-17-2014, 04:56 PM   #6
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 132

Original Poster
Rep: Reputation: Disabled
ahh thanks, I didn't see the other bit of the website with the explanation.. I'll read that as well

I'll try adding the echo code and see what I get from the server.

I did add set -xv to the top of the script to try to see what was going on, but it just displayed the script and didn't appear to get stuck anywhere. I'll try what you've suggested and read through the link and see where I get to and will post back my progress.

Thanks again for your help
Jon
 
Old 06-18-2014, 04:35 AM   #7
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 132

Original Poster
Rep: Reputation: Disabled
When I added the echo line to display the server response nothing happens again..

I've been playing around and the server does respond in a sense... as if I do the following in the shell I do get a response of sorts from the SMTP Server...

Code:
# exec 5<> /dev/tcp/mailserver1.bathspa.ac.uk/25
# cat >&5 <<END
> helo bathspa.ac.uk
> END
# cat <&5
220 bathspa.ac.uk Microsoft ESMTP MAIL Service, Version: 7.5.7601.17514 ready at  Wed, 18 Jun 2014 09:30:25 +0100
While I don't completely understand whether I've done the above correctly, it's just me playing around from what I've been reading, but the fact that we get the 220 response from the MS SMTP server means something is working lol.

So I know I can make a connection manually (albeit not do a lot lol).. so maybe from here I can work on figuring out why the other script does not work.. in theory lol. I think knowledge is my weakness at the moment..
 
Old 06-18-2014, 04:41 AM   #8
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 132

Original Poster
Rep: Reputation: Disabled
....looking at it though, the response is 220 which is successful connection. The response to HELO should be '250 OK' - so it's connecting to the server but not carrying out the command I typed.

Although that could be due to my incorrect use of the above..?
 
Old 06-18-2014, 06:35 AM   #9
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 132

Original Poster
Rep: Reputation: Disabled
I've pulled apart the above script and written my own very basic alternative just to try to simplify it and help me understand it more..

However, it still does not work.

The only server response I receives is 220 and no 250's..

Code:
#!/bin/bash

if [ $# -ne 2 ]; then
        echo "Usage: $0 ToAddress Message"
        exit 1
fi

from=root@bathspa.ac.uk
to=$1
domain=bathspa.ac.uk
mailserver=mailgateway.bathspa.ac.uk
message=$2

exec 5<>/dev/tcp/$mailserver/25
echo "HELO $domain\r\n" >&5
read -r temp <&5
echo $temp
echo "Mail From: $from\r\n" >&5
read -r temp <&5
echo $temp
echo "Rcpt To: $to\r\n" >&5
read -r temp <&5
echo $temp
echo "Data\r\n" >&5
read -r temp <&5
echo $temp
echo $message >&5
echo ".\r\n" >&5
read -r temp <&5
echo $temp
5>&-
5<&-
echo "Message Sent"
exit 0
 
Old 06-18-2014, 09:10 AM   #10
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 132

Original Poster
Rep: Reputation: Disabled
Another update as I've been on this all day

In my testing I've noticed that the commands need to be echo'd in the following format to be ready by the server:

Code:
echo -e "helo domain.com\r\n"
I guess this is to send the command and then issue a carriage return to allow the command to be processed by the server. But I do have another issue looming..

When I test with Telnet:

Code:
# telnet xxxxxxxxx.bathspa.ac.uk 25
Trying 172.19.0.22...
Connected to xxxxxxxxx.bathspa.ac.uk.
Escape character is '^]'.
220 bathspa.ac.uk Microsoft ESMTP MAIL Service, Version: 7.5.7601.17514 ready at  Wed, 18 Jun 2014 14:03:22 +0100
helo bathspa.ac.uk
250 bathspa.ac.uk Hello [172.19.1.67]
mail from: test@bathspa.ac.uk
250 2.1.0 test@bathspa.ac.uk....Sender OK
rcpt to: jon@jbut.eu
250 2.1.5 jon@jbut.eu
data
354 Start mail input; end with <CRLF>.<CRLF>
test
.
250 2.6.0 <XXXXXXXXXXXX18TFjn00002374@bathspa.ac.uk> Queued mail for delivery
quit
221 2.0.0 bathspa.ac.uk Service closing transmission channel
Connection closed by foreign host.
The above works fine and the mail is sent.

When I try the same, but using BASH:

Code:
# exec 9<>/dev/tcp/xxxxxxxxx.bathspa.ac.uk/25
# echo -e "helo bathspa.ac.uk\r\n" >&9
# echo -e "mail from: test@bathspa.ac.uk\r\n" >&9
# echo -e "rcpt to: jon@jbut.eu\r\n" >&9
# echo -e "data\r\n" >&9
# cat <&9
220 bathspa.ac.uk Microsoft ESMTP MAIL Service, Version: 7.5.7601.17514 ready at  Wed, 18 Jun 2014 14:06:31 +0100
250 bathspa.ac.uk Hello [172.19.1.67]
500 5.3.3 Unrecognized command
500 5.3.3 Unrecognized command
500 5.3.3 Unrecognized command
So I'm getting Unrecognized command for 'mail from' and 'rcpt to' when done using bash, but not with Telnet. I've tried obvious things like removing some spaces etc, but no joy.

Any ideas?

Thanks
Jon
 
Old 06-18-2014, 09:41 AM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
You missed the -n option, without it echo adds another newline.
Code:
echo -e "helo bathspa.ac.uk\r\n" >&9
# same as
echo -en "helo bathspa.ac.uk\r\n\n" >&9

#you want
echo -en "helo bathspa.ac.uk\r\n" >&9
# which is the same as
echo -e "helo bathspa.ac.uk\r" >&9
Oh, I just looked again at the original script. The reason nothing is happening is that it only defines a function to send mail, to actually send the mail you have to call the function:

Code:
#!/bin/bash
 
# Function to connect to SMTP and send mail to it
# returns:
# 0 - SUCCESS
# 1 - FAILURE: unable to complete session to DATA command
# 2 - FAILURE: problems after DATA command (server did not enqueue the message)
function bash_mail(){
  # Default configuration
  # Can be overridden by ARGV in order: TO, SUBJECT, MESSAGE, FROM, SERVER
  HOST=bathspa.ac.uk
  TO=${1:-'xxxxxx@bathspa.ac.uk'}
  SUBJECT=${2:-"Probleem ${HOSTNAME}"}
  MESSAGE=${3:-'Panic! Come to rescue.'}
  FROM=${4:-"root@$HOST"}
  SERVER=${5:-'mailxxxxx.bathspa.ac.uk'}
  ...
}

# now call the function
bash_mail
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP SMTP connection to gmail SMTP protocol violation ratcateme Programming 9 02-23-2009 04:58 AM
smtp auth with sendmail through smtp.sbcglobal.yahoo.com woodson2 Fedora 0 10-23-2008 03:55 PM
Binding SMTP service into IP tables, and redirecting SMTP to port 366 bamquest6 Linux - Software 1 09-24-2008 03:48 AM
bash: using /dev/tcp to connect to an smtp server: issues techguy Programming 4 09-20-2006 12:56 PM
bash command line smtp mail nutthick Programming 1 01-23-2005 03:23 PM


All times are GMT -5. The time now is 09:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration