はじめに
SMTPサーバの接続確認をするたびにコマンドを打つのは面倒なので、シェルスクリプト化してみた(今更感)
SMTPサーバの接続情報
- サーバ名: smtp.sendgrid.net
- ポート番号:
- 平文 or TLSを利用する場合:25 / 587 (今回は587を使用します。)
- SSLを利用する場合:465
認証情報
事前に、SendGridの認証情報(ユーザ名、パスワード)をBASE64エンコードする必要があります。
BASE64エンコードできるサイト:https://www.base64encode.org/
SMTPコマンドを手動で実行
コマンドをペチペチ叩く場合は以下のとおり
smtp
$ telnet smtp.sendgrid.net 587
$ EHLO
$ auth login
$ <SendGridのユーザ名をBASE64エンコードしたもの>
$ <SendGridのパスワードをBASE64エンコードしたもの>
$ mail from: from@domain.com
$ rcpt to: to@domain.com
$ data
$ To: to@domain.com
$ From: from@domain.com
$ Subject: SendGrid a message using Telnet:
$ <ENTER>
$ This is where the body of the message you would like to send goes.
$ .
$ quit
SMTPコマンドを自動で実行
mail.sh
#!/bin/zsh
SUBJECT="Sending a message using Telnet:";
BODY_MG="This is where the body of the message you would like to send goes.";
SMTP_HOST="smtp.sendgrid.net";
SMTP_PORT="587";
FROM_ADDR="from@domain.com";
TO_ADDR="to@domain.com";
SG_USER="SendGridのユーザ名をBASE64エンコードしたもの";
SG_PASS="SendGridのパスワードをBASE64エンコードしたもの";
(
echo EHLO; sleep 1;
echo auth login;
echo ${SG_USER};
echo ${SG_PASS}; sleep 1;
echo mail from: ${FROM_ADDR};
echo rcpt to: ${TO_ADDR};
echo data;
echo to: ${TO_ADDR};
echo from: ${FROM_ADDR};
echo subject: ${SUBJECT};
echo ;
echo ${BODY_MG};
echo .;
echo quit;
) | telnet ${SMTP_HOST} ${SMTP_PORT};
exit 0;
実行結果
mail.sh
$ ./mail.sh
Trying xxx.xx.xx.x...
Connected to smtp.sendgrid.net.
Escape character is '^]'.
220 SG ESMTP service ready
250-smtp.sendgrid.net
250-8BITMIME
250-PIPELINING
250-SIZE 31457280
250-STARTTLS
250-AUTH PLAIN LOGIN
250 AUTH=PLAIN LOGIN
334 xxxxxxxxxxxx
334 xxxxxxxxxxxx
235 Authentication successful
Connection closed by foreign host.