-
Notifications
You must be signed in to change notification settings - Fork 18
/
mail.lua
60 lines (54 loc) · 1.53 KB
/
mail.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
--------------------------------------------------------------------------------
-- mail.lua, v0.2: sends email messages
-- This file is a part of Sailor project
-- Copyright (c) 2014 Etiene Dalcol <[email protected]>
-- License: MIT
-- http://sailorproject.org
--------------------------------------------------------------------------------
local mail = {}
local socket = require 'socket'
local smtp = require 'socket.smtp'
local ssl = require 'ssl'
local ltn12 = require 'ltn12'
local conf = require('conf.conf').smtp
function mail.ssl_create()
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 mail.send_message(to, subject, body)
local msg = {
headers = {
to = to,
subject = subject
},
body = body
}
local ok, err = smtp.send {
from = conf.from,
rcpt = to,
source = smtp.message(msg),
user = conf.user,
password = conf.pass,
server = conf.server,
port = 465,
create = mail.ssl_create
}
if not ok then
return false, "SMTP settings failed: "..err.."."
end
return ok
end
return mail