电子邮件 – lua用gmail帐户发送邮件

前端之家收集整理的这篇文章主要介绍了电子邮件 – lua用gmail帐户发送邮件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想用我的Gmail帐户发送电子邮件,我试一试,但没有运气,所以有人可以给我一个样品吗?任何建议,将不胜感激.谢谢

我使用了lualogging api,代码

require"logging.email"

logger = logging.email {
  rcpt = "aaa@sina.com",from = "bbb@gmail.com",user = "bbb@gmail.com",password = *****,server = "smtp.gmail.com",port = 587,headers = { 
    rcpt = "aaa@sina.com",subject = "[%level] logging.email test",},}

logger:error("error!")

解决方法

您应该查看 LuaSocket,尤其是 SMTP module,它可以用于使用您的GMail帐户发送邮件.您还需要一个SSL库,我使用的是 LuaSec,它与LuaSocket一起使用.这是我使用我的GMail帐户成功发送电子邮件代码
-- Michal Kottman,2011,public domain
local socket = require 'socket'
local smtp = require 'socket.smtp'
local ssl = require 'ssl'
local https = require 'ssl.https'
local ltn12 = require 'ltn12'

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

function sendMessage(subject,body)
    local msg = {
        headers = {
            to = 'Your Target <target email>',subject = subject
        },body = body
    }

    local ok,err = smtp.send {
        from = '<your email>',rcpt = '<target email>',source = smtp.message(msg),user = 'username',password = 'password',server = 'smtp.gmail.com',port = 465,create = sslCreate
    }
    if not ok then
        print("Mail send Failed",err) -- better error handling required
    end
end
原文链接:https://www.f2er.com/lua/274532.html

猜你在找的Lua相关文章