コンテンツへスキップ
🛠️ToolsShed

SMTP Commands Reference

SMTPコマンド、レスポンスコード、インタラクティブな例を含む電子メールプロトコルフローの完全なリファレンスです。

EHLOsession
EHLO <hostname>

Extended greeting; identifies the client and requests ESMTP extensions.

EHLO mail.example.com
HELOsession
HELO <hostname>

Basic SMTP greeting. Use EHLO for modern servers.

HELO mail.example.com
QUITsession
QUIT

Close the SMTP connection gracefully.

QUIT
STARTTLSsession
STARTTLS

Upgrade the connection to TLS encryption.

STARTTLS
NOOPsession
NOOP

No operation; used to keep the connection alive.

NOOP
RSETsession
RSET

Abort the current mail transaction and reset the session.

RSET
AUTHauth
AUTH <mechanism> [credentials]

Authenticate the client to the server (PLAIN, LOGIN, CRAM-MD5).

AUTH LOGIN
MAIL FROMmessage
MAIL FROM:<address>

Specify the sender envelope address.

MAIL FROM:<sender@example.com>
RCPT TOmessage
RCPT TO:<address>

Specify a recipient envelope address. Can be repeated.

RCPT TO:<recipient@example.com>
DATAmessage
DATA

Begin transmitting the message body. End with a line containing only a period.

DATA
VRFYsession
VRFY <address>

Verify that a mailbox exists (often disabled for security).

VRFY user@example.com
EXPNsession
EXPN <list>

Expand a mailing list (often disabled for security).

EXPN users

このツールについて

SMTP(Simple Mail Transfer Protocol)は、メールサーバーがインターネット上でメールを中継・配送するために使うテキストベースのプロトコルです。メール機能を組み込む開発者も、配送の問題を調査するシステム管理者も、その基本コマンドを正しく理解しておく必要があります。

このリファレンスでは、HELO/EHLO、MAIL FROM、RCPT TO、DATA、STARTTLS、AUTH、QUIT といった主要な SMTP コマンドを、構文・簡単な説明・例とともに一覧化しています。telnet や openssl を使った手動セッションを順に試したり、メッセージが拒否された原因を調べたり、プロトコルの仕組みを段階的に学んだりするのに役立ちます。

最近のメールサーバーは、暗号化のための STARTTLS と送信認証のための AUTH をほぼ必須としているため、平文の無認証セッションは拒否されることが多い点に注意してください。本ページは本番のメール設定ガイドではなく、学習・参照用の補助としてご利用ください。

よくある質問

コード実装

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

def send_email_smtp(
    smtp_host: str,
    smtp_port: int,
    username: str,
    password: str,
    from_addr: str,
    to_addr: str,
    subject: str,
    body: str,
    use_tls: bool = True
) -> bool:
    """Send email using SMTP with manual command flow."""
    try:
        with smtplib.SMTP(smtp_host, smtp_port) as server:
            server.ehlo()  # EHLO command
            if use_tls:
                server.starttls()  # STARTTLS command
                server.ehlo()     # Re-EHLO after TLS
            server.login(username, password)  # AUTH command

            msg = MIMEMultipart()
            msg['From'] = from_addr
            msg['To'] = to_addr
            msg['Subject'] = subject
            msg.attach(MIMEText(body, 'plain'))

            # MAIL FROM, RCPT TO, DATA commands
            server.sendmail(from_addr, to_addr, msg.as_string())
            return True
    except smtplib.SMTPException as e:
        print(f"SMTP Error: {e}")
        return False

# Test SMTP connection manually
def test_smtp_connection(host: str, port: int) -> dict:
    import socket
    try:
        with smtplib.SMTP(host, port, timeout=5) as server:
            banner = server.getwelcome()
            ehlo_resp = server.ehlo()
            return {
                "connected": True,
                "banner": banner.decode(),
                "extensions": list(server.esmtp_features.keys())
            }
    except Exception as e:
        return {"connected": False, "error": str(e)}

result = test_smtp_connection("smtp.gmail.com", 587)
print(result)

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.