Mailscript with netcat: Unterschied zwischen den Versionen

Aus Xinux Wiki
Zur Navigation springen Zur Suche springen
Zeile 1: Zeile 1:
<pre>
 
 
#!/bin/bash
 
#!/bin/bash
# script to send test mail with netcat.
+
test "$#" -eq 5 || exit
# expects the following arguments:
+
SAVE=$IFS
# 1. recepient mail server
+
FROM=$3
# 2. port (typically 25 or 465)
+
TO=$4
# 3. mail from (e.g. from@example.com)
+
SERVER=$1#!/bin/bash
# 4. mail to (e.g. to@example.com)
+
test "$#" -eq 5 || exit
+
SAVE=$IFS
# for mail_input function
+
FROM=$3
from=$3
+
TO=$4
to=$4
+
SERVER=$1
+
PORT=$2
# error handling
+
SUBJECT=$5
function err_exit { echo -e 1>&2; exit 1; }
+
IFS=" "
+
DATA=$(cat)
# check if proper arguments are supplied
+
function mail()
if [ $# -ne 4 ]; then
+
{
  echo -e "\n Usage error!"
+
cat<<HERE
  echo " This script requires four arguments:"
+
ehlo $(hostname -f)
  echo " 1. recepient mail server"
+
MAIL FROM: <$FROM>
  echo " 2. port (typically 25 or 465)"
+
RCPT TO: <$TO>
  echo " 3. mail from (e.g. from@example.com)"
+
DATA
  echo " 4. mail to (e.g. to@example.com)"
+
From: <$FROM>
  exit 1
+
To: <$TO>
fi
+
Subject: $SUBJECT
+
 
# create message
+
$DATA
function mail_input {
+
.
  echo "ehlo $(hostname -f)"
+
quit
  echo "MAIL FROM: <$from>"
+
HERE
  echo "RCPT TO: <$to>"
 
  echo "DATA"
 
  echo "From: <$from>"
 
  echo "To: <$to>"
 
  echo "Subject: Testing one two three"
 
  echo "This is only a test. Please do not panic. If this works, then all is well, else all is not well."
 
  echo "In closing, Lorem ipsum dolor sit amet, consectetur adipiscing elit."
 
  echo "."
 
  echo "quit"
 
 
}
 
}
+
mail | nc $SERVER $PORT
# test
+
IFS=$SAVE
#mail_input
+
 
+
PORT=$2
# send
+
SUBJECT=$5
mail_input | nc $1 $2 || err_exit
+
IFS=" "
</pre>
+
DATA=$(cat)
 +
function mail()
 +
{
 +
cat<<HERE
 +
ehlo $(hostname -f)
 +
MAIL FROM: <$FROM>
 +
RCPT TO: <$TO>
 +
DATA
 +
From: <$FROM>
 +
To: <$TO>
 +
Subject: $SUBJECT
 +
 
 +
$DATA
 +
.
 +
quit
 +
HERE
 +
}
 +
mail | nc $SERVER $PORT
 +
IFS=$SAVE

Version vom 12. September 2014, 09:45 Uhr

  1. !/bin/bash

test "$#" -eq 5 || exit SAVE=$IFS FROM=$3 TO=$4 SERVER=$1#!/bin/bash test "$#" -eq 5 || exit SAVE=$IFS FROM=$3 TO=$4 SERVER=$1 PORT=$2 SUBJECT=$5 IFS=" " DATA=$(cat) function mail() { cat<<HERE ehlo $(hostname -f) MAIL FROM: <$FROM> RCPT TO: <$TO> DATA From: <$FROM> To: <$TO> Subject: $SUBJECT

$DATA . quit HERE } mail | nc $SERVER $PORT IFS=$SAVE

PORT=$2 SUBJECT=$5 IFS=" " DATA=$(cat) function mail() { cat<<HERE ehlo $(hostname -f) MAIL FROM: <$FROM> RCPT TO: <$TO> DATA From: <$FROM> To: <$TO> Subject: $SUBJECT

$DATA . quit HERE } mail | nc $SERVER $PORT IFS=$SAVE