File: mail.sh

package info (click to toggle)
socat 1.4.2.0-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,144 kB
  • ctags: 3,624
  • sloc: ansic: 18,730; sh: 3,319; makefile: 153
file content (70 lines) | stat: -rwxr-xr-x 1,801 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#! /bin/sh
# $Id: mail.sh,v 1.10 2004/06/06 17:33:22 gerhard Exp $
# Copyright Gerhard Rieger 2001-2003
# Published under the GNU General Public License V.2, see file COPYING

#set -vx

# This is an example for a shell script that can be fed to socat with exec.
# Its clue is that it does not use stdin/stdout for communication with socat,
# so you may feed the mail message via stdin to the script. The message should
# contain appropriate mail headers.
# Lines with only a dot are not permitted - use two dots as escape.
# This script supports multiline answers from server, but not much more yet.

# Usage:  cat message.txt |socat exec:"mail.sh target@domain.com",fdin=3,fdout=4 tcp:mail.relay.org:25,crlf

while [ "$1" ]; do
    case "$1" in
    -f) shift; mailfrom="$1"; shift;;
    *) break;;
    esac
done

rcptto="$1"
[ -z "$1" ] && rcptto="root@loopback"
#server=$(expr "$rcptto" : '[^@]*@\(.*\)')
[ -z "$mailfrom" ] && mailfrom="$USER@$(hostname)"

# this function waits for a complete server message, checks if its status
# is in the permitted range (terminates session if not), and returns.
mail_chat () {
    local cmd="$1" 
    local errlevel="$2";  [ -z "$errlevel" ] && errlevel=300

    echo "> $cmd" >&2
    if [ -n "$cmd" ]; then echo "$cmd" >&4; fi
    while read status message <&3;
	(
	    case "$status" in
	    [0-9][0-9][0-9]-*) exit 0;;
	    [0-9][0-9][0-9]*) exit 1;;
	    *) exit 0;;
	    esac
	)
    do :; done
    echo "< $status $message" >&2
    if [ "$status" -ge "$errlevel" ]; then
	echo $message >&2
	echo "QUIT" >&4; exit 1
    fi
}


# expect server greeting
mail_chat

mail_chat "HELO $(hostname)"

mail_chat "MAIL FROM: $mailfrom"

mail_chat "RCPT TO: $rcptto"

mail_chat "DATA" 400

while read l; do echo "$l" >&4; done
mail_chat "."

mail_chat "QUIT"

exit 0