lib_smtp.lua 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. local lib_smtp = {}
  2. lib_smtp.socket_debug_enable = false
  3. lib_smtp.packet_size = 512
  4. lib_smtp.timeout = 1000 * 30
  5. --- 日志格式化函数
  6. -- @param content string, 日志内容
  7. -- @return string, 处理后的日志内容
  8. local function logFormat(content)
  9. -- 隐藏 AUTH 用户信息
  10. content = content:gsub("AUTH PLAIN (.-)\r\n", "AUTH PLAIN ***\r\n")
  11. -- 替换换行符
  12. content = content:gsub("\r", "\\r"):gsub("\n", "\\n")
  13. -- 截取
  14. content = content:sub(1, 200) .. (#content > 200 and " ..." or "")
  15. return content
  16. end
  17. --- 转义句号函数
  18. -- @param content string, 需要转义的内容
  19. -- @return string, 转义后的内容
  20. local function escapeDot(content)
  21. return content:gsub("(.-\r\n)", function(line)
  22. if line:sub(1, 1) == "." then line = "." .. line end
  23. return line
  24. end)
  25. end
  26. --- 接收到数据时的处理函数
  27. -- @param netc userdata, socket.create 返回的 netc
  28. -- @param rxbuf userdata, 接收到的数据
  29. -- @param socket_id string, socket id
  30. -- @param current_command string, 当前要发送的命令
  31. local function recvHandler(netc, rxbuf, socket_id, current_command)
  32. local rx_str = rxbuf:toStr(0, rxbuf:used())
  33. log.info("lib_smtp", socket_id, "<-", logFormat(rx_str))
  34. -- 如果返回非 2xx 或 3xx 状态码, 则断开连接
  35. if not rx_str:match("^[23]%d%d") then
  36. log.error("lib_smtp", socket_id, "服务器返回错误状态码, 断开连接, 请检查日志")
  37. sys.publish(socket_id .. "_disconnect", { success = false, message = "服务器返回错误状态码", is_retry = false })
  38. return
  39. end
  40. if current_command == nil then
  41. log.info("lib_smtp", socket_id, "全部发送完成")
  42. sys.publish(socket_id .. "_disconnect", { success = true, message = "发送成功", is_retry = false })
  43. return
  44. end
  45. -- 分包发送
  46. local index = 1
  47. sys.taskInit(function()
  48. while index <= #current_command do
  49. local packet = current_command:sub(index, index + lib_smtp.packet_size - 1)
  50. socket.tx(netc, packet)
  51. log.info("lib_smtp", socket_id, "->", logFormat(packet))
  52. index = index + lib_smtp.packet_size
  53. sys.wait(100)
  54. end
  55. end)
  56. end
  57. local function validateParameters(smtp_config)
  58. -- 配置参数验证规则
  59. local validation_rules = {
  60. { field = "host", type = "string", required = true },
  61. { field = "port", type = "number", required = true },
  62. { field = "username", type = "string", required = true },
  63. { field = "password", type = "string", required = true },
  64. { field = "mail_from", type = "string", required = true },
  65. { field = "mail_to", type = "string", required = true },
  66. { field = "tls_enable", type = "boolean", required = false },
  67. }
  68. local result = true
  69. for _, rule in ipairs(validation_rules) do
  70. local value = smtp_config[rule.field]
  71. if rule.type == "string" and (value == nil or value == "") then
  72. log.error("lib_smtp", string.format("`smtp_config.%s` 应为非空字符串", rule.field))
  73. result = false
  74. elseif rule.required and type(value) ~= rule.type then
  75. log.error("lib_smtp", string.format("`smtp_config.%s` 应为 %s 类型", rule.field, rule.type))
  76. result = false
  77. end
  78. end
  79. return result
  80. end
  81. --- 发送邮件
  82. -- @param body string 邮件正文
  83. -- @param subject string 邮件主题
  84. -- @param smtp_config table 配置参数
  85. -- - smtp_config.host string SMTP 服务器地址
  86. -- - smtp_config.username string SMTP 账号用户名
  87. -- - smtp_config.password string SMTP 账号密码
  88. -- - smtp_config.mail_from string 发件人邮箱地址
  89. -- - smtp_config.mail_to string 收件人邮箱地址
  90. -- - smtp_config.port number SMTP 服务器端口号
  91. -- - smtp_config.tls_enable boolean 是否启用 TLS(可选,默认为 false)
  92. -- @return result table 发送结果
  93. -- - result.success boolean 是否发送成功
  94. -- - result.message string 发送结果描述
  95. -- - result.is_retry boolean 是否需要重试
  96. function lib_smtp.send(body, subject, smtp_config)
  97. -- 参数验证
  98. if type(smtp_config) ~= "table" then
  99. log.error("lib_smtp", "`smtp_config` 应为 table 类型")
  100. return { success = false, message = "参数错误", is_retry = false }
  101. end
  102. local valid = validateParameters(smtp_config)
  103. if not valid then return { success = false, message = "参数错误", is_retry = false } end
  104. subject = type(subject) == "string" and subject or ""
  105. body = type(body) == "string" and escapeDot(body) or ""
  106. lib_smtp.send_count = (lib_smtp.send_count or 0) + 1
  107. local socket_id = "socket_" .. lib_smtp.send_count
  108. local rxbuf = zbuff.create(256)
  109. local commands = {
  110. "HELO " .. smtp_config.host .. "\r\n",
  111. "AUTH PLAIN " .. string.toBase64("\0" .. smtp_config.username .. "\0" .. smtp_config.password) .. "\r\n",
  112. "MAIL FROM: <" .. smtp_config.mail_from .. ">\r\n",
  113. "RCPT TO: <" .. smtp_config.mail_to .. ">\r\n",
  114. "DATA\r\n",
  115. table.concat({
  116. "From: " .. smtp_config.mail_from,
  117. "To: " .. smtp_config.mail_to,
  118. "Subject: " .. subject,
  119. "Content-Type: text/plain; charset=UTF-8",
  120. "",
  121. body,
  122. ".",
  123. "",
  124. }, "\r\n"),
  125. }
  126. local current_command_index = 1
  127. local function getNextCommand()
  128. local command = commands[current_command_index]
  129. current_command_index = current_command_index + 1
  130. return command
  131. end
  132. -- socket 回调
  133. local function netCB(netc, event, param)
  134. if param ~= 0 then
  135. sys.publish(socket_id .. "_disconnect", { success = false, message = "param~=0", is_retry = true })
  136. return
  137. end
  138. if event == socket.LINK then
  139. log.info("lib_smtp", socket_id, "LINK")
  140. elseif event == socket.ON_LINE then
  141. log.info("lib_smtp", socket_id, "ON_LINE")
  142. elseif event == socket.EVENT then
  143. socket.rx(netc, rxbuf)
  144. socket.wait(netc)
  145. if rxbuf:used() > 0 then recvHandler(netc, rxbuf, socket_id, getNextCommand()) end
  146. rxbuf:del()
  147. elseif event == socket.TX_OK then
  148. socket.wait(netc)
  149. elseif event == socket.CLOSE then
  150. log.info("lib_smtp", socket_id, "CLOSED")
  151. sys.publish(socket_id .. "_disconnect", { success = false, message = "服务器断开连接", is_retry = true })
  152. end
  153. end
  154. -- 初始化 socket
  155. local netc = socket.create(nil, netCB)
  156. socket.debug(netc, lib_smtp.socket_debug_enable)
  157. socket.config(netc, nil, nil, smtp_config.tls_enable)
  158. -- 连接 smtp 服务器
  159. local is_connect_success = socket.connect(netc, smtp_config.host, smtp_config.port)
  160. if not is_connect_success then
  161. socket.close(netc)
  162. return { success = false, message = "未知错误", is_retry = true }
  163. end
  164. -- 等待发送结果
  165. local is_send_success, send_result = sys.waitUntil(socket_id .. "_disconnect", lib_smtp.timeout)
  166. socket.close(netc)
  167. if is_send_success then
  168. return send_result
  169. else
  170. log.error("lib_smtp", socket_id, "发送超时")
  171. return { success = false, message = "发送超时", is_retry = true }
  172. end
  173. end
  174. return lib_smtp