LoginSignup
0
1

Telnetでメールを送るスクリプト

Last updated at Posted at 2023-06-02

はじめに

SMTPサーバまたはSMTPのようなプログラムを立てた時、ローカルから Telnet コマンドでメールを流してテストしたい時があります。しかし、Telnet 操作中には誤字が入ったり間違ったりすることも多いので簡単にスクリプト化したくなります。一番簡単な text/plain メールや、テキストの添付ファイルが付いたメールを送るスクリプトを紹介します。

シンプルなテキストメール(text/plain)を送りたい

Usage

./mailsend.sh | telnet 127.0.0.1 25

mailsend.shの中身

#!/bin/sh

echo "HELO 127.0.0.1"
sleep 1
echo "MAIL FROM: test@example.com"
sleep 1
echo "RCPT TO: test@example.com"
sleep 1
echo "DATA"
sleep 1
echo "From:test@example.com"
echo "To:test@example.com"
echo "Subject: Test Mail"
echo ""
echo "test"
echo "."
sleep 1
echo "QUIT"

添付ファイルが付いているメール(multipart/mixed)を送りたい

添付ファイルが付いた multipart/mixed メールだと、ヘッダにboundaryやcontent-typeなどのヘッダが必要になります。

usage

./mailsend_attach.sh | telnet 127.0.0.1 25

./mailsend_attach.shの中身

#!/bin/sh

echo "HELO 127.0.0.1"
sleep 1
echo "MAIL FROM:test@example.com"
sleep 1
echo "RCPT TO: test@example.com"
sleep 1
echo "DATA"
sleep 1
echo 'MIME-Version:1.0'
echo "From:test@example.com"
echo "To:test@example.com"
echo "Subject: Test Mail for Attachment"
echo 'Content-Type: multipart/mixed; boundary="-----VM2CLL6AQ4USQX4JOG3XAB1A795I
F0B_0_000045AD"'
echo ""
echo "This is a multi-part message in MIME format."
echo "-------VM2CLL6AQ4USQX4JOG3XAB1A795IF0B_0_000045AD"
echo "Content-Type: text/plain; charset=US-ASCII"
echo "Content-Transfer-Encoding: 7bit"
echo "test"
echo ""
echo "-------VM2CLL6AQ4USQX4JOG3XAB1A795IF0B_0_000045AD"
echo 'Content-Type: text/plain; name="test.txt"'
echo "Content-Transfer-Encoding: base64"
echo 'Content-Disposition: attachment;filename="test.txt"'
echo ""
echo "dGVzdCB0ZXN0"
echo "-------VM2CLL6AQ4USQX4JOG3XAB1A795IF0B_0_000045AD--"
echo "."
sleep 1
echo "QUIT"

git cloneも可能です。

https://github.com/mihyon/mailsend.git
0
1
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
0
1