lua发送邮件与Gmail帐户 [英] lua send mail with gmail account
本文介绍了lua发送邮件与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!)
解决方案
你应该看看 LuaSocket < a>,特别是其可用于发送邮件的 SMTP模块使用您的GMail帐户。您还需要一个SSL库,我使用的设计是与LuaSocket一起使用的 LuaSec 。这是我成功使用我的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)
如果不是r然后返回r,e结束
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
函数sendMessage(subject,body)
local msg = {
headers = {
to ='您的目标<目标电子邮件>',
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
}
如果不行,然后
打印(邮件发送失败,错误) - 更好的错误处理需要
结束
结束
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
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!")
解决方案
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屋!
查看全文