LoginSignup
1
1

nc(もしくはtelnet)を使ってmacOSやLinuxのコマンドラインからSMTP認証を通したメール送信を行う

Last updated at Posted at 2023-07-27
send_mail.sh
#!/bin/bash
# SMTP認証を利用したメール送信を行います。

# set -xe

# 下記を書き換えてください。
HOST=example.com
PORT=587
USER=info@example.com # SMTP認証に利用されるメールアドレス
PASS=somethingpass
TO=yousan@example.com
FROM=info@exmaple.com

# SMTP認証用の文字列を作成します。
# @see https://www.tohoho-web.com/ex/draft/smtp-auth.htm
SMTP_AUTH_STR=$(printf "%s\0%s\0%s" $USER $USER $PASS | openssl base64 -e | tr -d '\n'; echo)

function mail_input {
  echo "EHLO localhost"
  sleep 1
  echo "auth plain $SMTP_AUTH_STR"
  echo "mail from: $FROM"
  echo "rcpt to: $TO"
  echo "data"
  echo "To: $TO"
  echo "From: $FROM"
  echo "Subject: Hello Mail Server"
  echo ""
  echo "test mail"
  echo "."
  echo "quit"
}

# send
mail_input | nc ${HOST} $PORT

下記のページを参考にしました。

1
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
1
1