lua 使用 gmail 帐户发送邮件 [英] lua send mail with gmail account
本文介绍了lua 使用 gmail 帐户发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用我的 gmail 帐户发送电子邮件,我试了一下,但没有成功,所以有人可以给我一个样本吗?任何建议,将不胜感激.谢谢
I want to send email with my gmail account, I gave it a try, but no luck, so is anyone can give me a sample? Any suggestions would be appreciated. Thank you
我用的是lualogging api,代码是
I used lualogging api, the code is
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!")
推荐答案
你应该看看 LuaSocket,尤其是它的SMTP 模块,可以使用使用您的 GMail 帐户发送邮件.您还需要一个 SSL 库,我使用 LuaSec,它旨在与 LuaSocket 一起使用.这是我使用 GMail 帐户成功发送电子邮件的代码:
You should look at LuaSocket, especially its SMTP module which can be used to send mail using your GMail account. You also need a SSL library, I use LuaSec which was designed to be used together with LuaSocket. This is the code I successfully used to send emails using my GMail account:
-- 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
这篇关于lua 使用 gmail 帐户发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文