我想用我的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",
from = "bbb@gmail.com",
subject = "[%level] logging.email test",
},
}
logger:error("error!")
答案 0 :(得分:14)
您应该查看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
答案 1 :(得分:0)
Michal Kottman的代码工作正常,但是当smpt服务器在587端口上工作时,它失败了(对我来说),使用一种非常不同的方式来接收要发送的邮件(根据我所读的内容)。有人遇到过类似的事吗?我总是得到&#34;错误的版本号&#34;在端口587上工作的服务器上。