lua socket 发送邮件时出现“smtp.lua:80: attempt to call field 'b64' (a nil value)”错误

尝试使用 luasocket 的 smtp 函数和 ssl 发送邮件,但是出现了这个错误 /usr/local/share/lua/5.1/socket/smtp.lua:80: attempt to call field 'b64' (a nil value)。我已经下载了所有的库,但是不知道为什么不起作用。 这是我的代码

local smtp = require("socket.smtp")
local ssl = require('ssl')
local https = require 'ssl.https'
local mime = require("mime")
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 k, e = smtp.send{
                from = "[REDACTED]",
                rcpt = self.params.email,
                user = "[REDACTED]",
                password = "[REDACTED]",
                port = 465,
                server = "smtp.gmail.com",
                source = smtp.message(message),
                create = sslCreate
            }
            if not k then
                print(e)
            end
点赞
用户1442917
用户1442917

代码中第80行调用了mime.b64()函数,而mime是通过require "mime"调用获得的结果(mime模块来自于luasocket库)。除非mime模块本身有问题(如果它来自正确的源并且正确安装,就不应该有问题),否则它很可能是由于某个在package.path的地方可用的mime.lua文件而引起的,所以它被加载代替了实际的模块。

如果你想进一步调试它,只需在调试器中查看require"mime"的结果或使用package.searchpath("mime", package.path)来查看此时哪个被选中(searchpath);您可能还需要尝试使用package.cpath

2021-06-09 23:46:36