Last active
March 16, 2019 18:02
-
-
Save agentzh/d9288e4dd88941fc685598b98f74766a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local account = "[email protected]" -- use your own gmail account | |
local password = "password" -- if you enable 2-phase authentication, you need to | |
-- generate and use a application-specific password here... | |
local sender_name = "Jon Snow" | |
local recipient = "[email protected]" | |
local recipient_name = "Arya Stark" | |
-------------------------------------------------------- | |
local find = ngx.re.find | |
local gsub = ngx.re.gsub | |
local sub = string.sub | |
local tostring = tostring | |
local tonumber = tonumber | |
local encode_base64 = ngx.encode_base64 | |
local sock | |
local function send_cmd(cmd) | |
assert(sock:send(cmd .. "\r\n")) | |
ngx.say(">> ", cmd) | |
do | |
local lines = {} | |
local i = 0 | |
local line = assert(sock:receive()) | |
while true do | |
local fr, to, err = find(line, [[^(\d+)-]], "jo", nil, 1) | |
if err then | |
error("failed to call ngx.re.find: " .. tostring(err)) | |
end | |
if not fr then | |
break | |
end | |
local status = tonumber(sub(line, fr, to)) | |
if status >= 400 then | |
return nil, line | |
end | |
i = i + 1 | |
lines[i] = line | |
line = assert(sock:receive()) | |
end | |
local fr, to, err = find(line, [[^(\d+) ]], "jo", nil, 1) | |
if err then | |
error("failed to call ngx.re.find: " .. tostring(err)) | |
end | |
if not fr then | |
error("syntax error: " .. line); | |
end | |
local status = tonumber(sub(line, fr, to)) | |
if status >= 400 then | |
return nil, line | |
end | |
i = i + 1 | |
lines[i] = line | |
ngx.say(table.concat(lines, "\n")) | |
end | |
return true | |
end | |
sock = ngx.socket.tcp() | |
assert(sock:connect("smtp.gmail.com", 465)) | |
assert(sock:sslhandshake()) | |
local line = assert(sock:receive()) | |
ngx.say(line) | |
assert(send_cmd("EHLO smtp.gmail.com")) -- well, we could verify the SSL handshake here... | |
local auth = ngx.encode_base64("\0" .. account .. "\0" .. password) | |
assert(send_cmd("AUTH PLAIN " .. auth)) | |
assert(send_cmd("MAIL FROM:<" .. account .. ">")) -- well, you could use a different email address than | |
-- the one specified in the account variable if you | |
-- have configured alternative "send email as" entries | |
-- in your gmail account | |
assert(send_cmd("RCPT TO:<" .. recipient .. ">")) | |
assert(sock:send("DATA\r\n")) | |
assert(sock:send("FROM: " .. sender_name .. " <" .. account .. ">\r\n")) | |
assert(sock:send("Subject: " .. mail_title .. "\r\n")) | |
assert(sock:send("To: " .. recipient_name .. " <" .. recipient .. "\r\n")) | |
assert(sock:send("Content-Transfer-Encoding: base64\r\n")) | |
assert(sock:send("Content-Type: text/html; charset=utf-8\r\n\r\n")) | |
assert(sock:send(gsub(encode_base64(mail_body), [[(.{76})]], "$1\n", "jo") .. "\n")) | |
assert(send_cmd("\r\n.")) | |
assert(send_cmd("QUIT")) | |
assert(sock:close()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
root@lb-waf:/etc/apache2# lua sendmailss.lua
lua: sendmailss.lua:10: attempt to index global 'ngx' (a nil value)
stack traceback:
sendmailss.lua:10: in main chunk
[C]: in ?
help please