"""Offline Ruby 3.x semantic verification lab for Chapter 39 (sending-email)."""
import base64
from email import message_from_string
import json
VERSION = "CPython 3.12.14 offline Ruby 3.x semantic verifier (MRI ruby absent, no live SMTP)"
def case_smtp_config_and_plain_message():
raw_plain = (
"From: Private Person <me@example.com>\r\n"
"To: A Test User <test@example.com>\r\n"
"Subject: SMTP e-mail test\r\n"
"\r\n"
"This is a test e-mail message.\r\n"
)
msg = message_from_string(raw_plain)
defaults = {"server": "localhost", "port": 25, "authtypes": ["plain", "login", "cram_md5"]}
expected = {
"default_port": 25,
"from": "Private Person <me@example.com>",
"to": "A Test User <test@example.com>",
"subject": "SMTP e-mail test",
"body": "This is a test e-mail message.",
"has_blank_line_sep": True,
"supported_auth_count": 3,
}
actual = {
"default_port": defaults["port"],
"from": msg["From"],
"to": msg["To"],
"subject": msg["Subject"],
"body": msg.get_payload().strip(),
"has_blank_line_sep": "\r\n\r\n" in raw_plain,
"supported_auth_count": len(defaults["authtypes"]),
}
return {
"name": "smtp_config_and_plain_message",
"summary": "核验 Net::SMTP 默认连接参数、认证方式及纯文本邮件 RFC 5322 头体空行分隔解析",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_smtp_html_mime_message():
raw_html = (
"From: Private Person <me@example.com>\r\n"
"To: A Test User <test@example.com>\r\n"
"MIME-Version: 1.0\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Subject: SMTP HTML test\r\n"
"\r\n"
"<b>This is HTML message.</b>\r\n"
"<h1>This is headline.</h1>\r\n"
)
msg = message_from_string(raw_html)
expected = {
"mime_version": "1.0",
"content_type": "text/html",
"charset": "utf-8",
"has_h1": True,
}
actual = {
"mime_version": msg["MIME-Version"],
"content_type": msg.get_content_type(),
"charset": msg.get_content_charset(),
"has_h1": "<h1>This is headline.</h1>" in msg.get_payload(),
}
return {
"name": "smtp_html_mime_message",
"summary": "核验 HTML 邮件中 MIME-Version、Content-Type 与 UTF-8 字符集声明及正文解析",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_smtp_multipart_base64_attachment():
marker = "AUNIQUEMARKER"
file_bytes = b"Ruby SMTP attachment sample.\n"
# Equivalent to Ruby [filecontent].pack("m")
encoded = base64.encodebytes(file_bytes).decode("ascii")
raw_multipart = (
f"From: Private Person <me@example.com>\r\n"
f"To: A Test User <test@example.com>\r\n"
f"Subject: Sending Attachment\r\n"
f"MIME-Version: 1.0\r\n"
f"Content-Type: multipart/mixed; boundary={marker}\r\n"
f"\r\n"
f"--{marker}\r\n"
f"Content-Type: text/plain; charset=UTF-8\r\n"
f"Content-Transfer-Encoding: 8bit\r\n"
f"\r\n"
f"This is a test email to send an attachment.\r\n"
f"--{marker}\r\n"
f'Content-Type: application/octet-stream; name="test.txt"\r\n'
f"Content-Transfer-Encoding: base64\r\n"
f'Content-Disposition: attachment; filename="test.txt"\r\n'
f"\r\n"
f"{encoded}"
f"--{marker}--\r\n"
)
msg = message_from_string(raw_multipart)
parts = msg.get_payload()
decoded_att = parts[1].get_payload(decode=True)
expected = {
"is_multipart": True,
"part_count": 2,
"part0_type": "text/plain",
"part1_filename": "test.txt",
"attachment_roundtrip": "Ruby SMTP attachment sample.\n",
}
actual = {
"is_multipart": msg.is_multipart(),
"part_count": len(parts),
"part0_type": parts[0].get_content_type(),
"part1_filename": parts[1].get_filename(),
"attachment_roundtrip": decoded_att.decode("utf-8"),
}
return {
"name": "smtp_multipart_base64_attachment",
"summary": "核验 multipart/mixed 边界分割、pack('m') 等价 Base64 编码与附件解码往返一致",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_smtp_config_and_plain_message(),
case_smtp_html_mime_message(),
case_smtp_multipart_base64_attachment(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装 MRI ruby 解释器)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装 MRI ruby 解释器)。
入口文件:projects/ruby-sending-email-lab/explore-sending-email.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-sending-email.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装 MRI ruby 解释器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联45章) · 含当前章节或实例 · mbskycom202612a2434.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2471.zip