2
2

bashだけでメール(smtp 25)を送るスクリプト

Posted at

リハビリテスト記事

必要なコマンドセット

bashcutだけ

スクリプト

#!/bin/bash -eux
 
_waitStatus(){
  while true;do
    read status <&3
    if [[ "$1" == "$(echo "$status" | cut -d' ' -f1)" ]];then
       break
    fi
  done
}

exec 3<> /dev/tcp/${MAILSERVERADDR}/25

_waitStatus 220

echo "HELO ${MAILSERVER}" >&3
_waitStatus 250

echo "MAIL FROM: <${MAILFROM}>" >&3
_waitStatus 250
 
echo "RCPT TO: <$1>" >&3
_waitStatus 250
 
echo "DATA" >&3
_waitStatus 354
 
echo "From: ${MAILFROM}" >&3
echo "Subject: $2" >&3
echo "" >&3
echo "$3" >&3
echo "." >&3

exec 3<&-

HowTo

下記のように実行する

$ export MAILSERVER=example.com
$ export MAILFROM=anya@example.com
$ export MAILSERVERADDR=smtp.example.com
$ ./sendmail.sh "rubbish@anya.jp" "Hello my bro!" "This is test mail at $(date)."

以上

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2