You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #! /bin/sh
  2. # source: mail.sh
  3. # Copyright Gerhard Rieger and contributors (see file CHANGES)
  4. # Published under the GNU General Public License V.2, see file COPYING
  5. #set -vx
  6. # This is an example for a shell script that can be fed to socat with exec.
  7. # Its clue is that it does not use stdin/stdout for communication with socat,
  8. # so you may feed the mail message via stdin to the script. The message should
  9. # contain appropriate mail headers without continuation lines.
  10. # socat establishes the connection to the SMTP server; the script performs the
  11. # SMTP dialog and afterwards transfers the message body to the server.
  12. # Lines with only a dot are not permitted - use two dots as escape.
  13. # This script supports multiline answers from server, but not much more yet.
  14. # Usage: cat message.txt |socat exec:"mail.sh target@domain.com",fdin=3,fdout=4 tcp:mail.relay.org:25,crlf
  15. while [ "$1" ]; do
  16. case "$1" in
  17. -f) shift; mailfrom="$1"; shift;;
  18. *) break;;
  19. esac
  20. done
  21. rcptto="$1"
  22. [ -z "$1" ] && rcptto="root@loopback"
  23. #server=$(expr "$rcptto" : '[^@]*@\(.*\)')
  24. [ -z "$mailfrom" ] && mailfrom="$USER@$(hostname)"
  25. # this function waits for a complete server message, checks if its status
  26. # is in the permitted range (terminates session if not), and returns.
  27. mail_chat () {
  28. local cmd="$1"
  29. local errlevel="$2"; [ -z "$errlevel" ] && errlevel=300
  30. if [ "$cmd" ]; then echo "> $cmd" >&2; fi
  31. if [ -n "$cmd" ]; then echo "$cmd" >&4; fi
  32. while read status message <&3;
  33. (
  34. case "$status" in
  35. [0-9][0-9][0-9]-*) exit 0;;
  36. [0-9][0-9][0-9]*) exit 1;;
  37. *) exit 1;;
  38. esac
  39. )
  40. do :; done
  41. if [ -z "$status" ]; then echo smtp connection failed >&2; exit; fi
  42. echo "< $status $message" >&2
  43. if [ "$status" -ge "$errlevel" ]; then
  44. echo $message >&2
  45. echo "QUIT" >&4; exit 1
  46. fi
  47. }
  48. # expect server greeting
  49. mail_chat
  50. mail_chat "HELO $(hostname)"
  51. mail_chat "MAIL FROM: $mailfrom"
  52. mail_chat "RCPT TO: $rcptto"
  53. mail_chat "DATA" 400
  54. while read l; do echo "$l" >&4; done
  55. mail_chat "."
  56. mail_chat "QUIT"
  57. exit 0