本帖最后由 zuyaosun 于 2021-11-12 16:18 编辑
function SendSimpleEmail(subject, body, em) -- 发送电子邮件(主题,内容,[收件人])
local socket = require "socket"
local smtp = require "socket.smtp"
local ssl = require "ssl"
local function sslCreate()
local sock = socket.tcp()
return setmetatable(
{
connect = function(_, host, port)
local r, e = sock:connect(host, port)
if not r then
return r, e
end
sock = ssl.wrap(sock, {mode = "client", protocol = "tlsv1"})
return sock:dohandshake()
end
},
{
__index = function(t, n)
return function(_, ...)
return sock[n](sock, ...)
end
end
}
)
end
local from = "<zuyaosun@163.com>" -- 发件人邮箱
local user = "zuyaosun@163.com" -- 用户
local password = "MUMLGLWLLTMFIPBL" -- 密码
local server = "smtp.163.com" -- 服务器
local port = 465 -- 端口
local rcpt = {"<312029928@qq.com>"} -- 收件人邮箱
if type(em) == "string" and string.find(em, "@") and string.find(em, "%.") then
table.insert(rcpt, "<" .. em .. ">")
end
local to = table.concat(rcpt, "")
--print(to)
local mesgt = {
headers = {
from = from,
to = to,
subject = subject
},
body = body
}
local r, e =
smtp.send {
from = from,
rcpt = rcpt,
source = smtp.message(mesgt),
user = user,
password = password,
server = server,
port = port,
create = sslCreate
}
if r then
return true
else
print("邮件发送失败", e) -- better error handling required
return false
end
end
print(SendSimpleEmail("这是主题", "ceshi", "312029928")) -- 发送电子邮件(主题,内容,[收件人])
--[===[ 注意使用的时候吧源码下方的变量 改成自己的
local from = "<zuyaosun@163.com>" -- 发件人邮箱
local user = "zuyaosun@163.com" -- 用户
local password = "MUMLGLWLLTMFIPBL" -- 密码
local server = "smtp.163.com" -- 服务器
local port = 465 -- 端口
local rcpt = {"<312029928@qq.com>"} -- 收件人邮箱]===]
|