px普贤教程PuXian
普贤教程 › 包参考

smtp 包

轻量 SMTP 客户端(邮件发送原语)

纯语言实现(tcp_connect 手写 SMTP 会话),编译/解释双模式一致(同步 TCP)。

项值
包名smtp
版本0.1.0
入口smtp.px
分类网络与协议
文件数 / 行数1 / 214

安装与引入

bash
pxpkg init          # 首次
pxpkg add smtp@0.1.0
pxpkg install
px
import smtp
所属分类教程:网络与协议

公开接口

函数参数
smtp_tryhost, port, from_addr, to_addrs, msg, opts
smtp_sendhost, port, from_addr, to_addrs, msg, opts
其余 12 个 def(内部与辅助,可直接调用但不属稳定接口)
函数参数
sm_isdigitc
sm_rfinds, needle
sm_rtrims
sm_last_linebuf
sm_reply_donebuf
sm_read_replyfd
sm_codebuf
sm_send_linefd, line
sm_dot_escapebody
sm_build_msgfrom_addr, to_list, msg
sm_to_listto
sm_donefd, ok, err

包内说明(源码头注释)

text
============================================================
普贤标准库:std.smtp —— 轻量 SMTP 客户端(邮件发送原语)
------------------------------------------------------------
纯语言实现(tcp_connect 手写 SMTP 会话),编译/解释双模式一致(同步 TCP)。
面向「告警通知 / 事务邮件」的轻量发信:
smtp_send(host, port, from, to, msg, opts?) → bool      # 成功 true
smtp_try (host, port, from, to, msg, opts?) → {ok, err} # 带诊断
from / to:邮箱地址(to 可 str 或 list[str])
msg:{"subject":..., "text":...} 或 {"subject":..., "html":...}
opts(可选 dict):{"user","password","helo"} —— 有 user 则 AUTH LOGIN;
默认明文(25/内网 587);STARTTLS 暂不支持(语言无
客户端 TLS 包装 native,见 M83_PLAN §S5 边界)
DATA 消息行首 "." 自动转义;长消息 tcp_send 循环发满。
⚠️ 不处理 RFC 2047 标题编码(非 ASCII Subject 请自行 base64 编码头);
Date 头由本库按服务器本地时间生成(多数 server 接受缺省/宽松格式)。
用法:
import std.smtp
if smtp_send("smtp.example.com", 25, "a@x.com", "b@y.com",
{"subject": "告警", "text": "disk 满"}, {"user": "u", "password": "p"}):
print("sent")
============================================================
---- 判断字符是否数字 ----