Skip to content

Instantly share code, notes, and snippets.

@jessequinn
Created May 8, 2019 20:15
Show Gist options
  • Save jessequinn/98eef93ee58be3dec193fb75561a35d8 to your computer and use it in GitHub Desktop.
Save jessequinn/98eef93ee58be3dec193fb75561a35d8 to your computer and use it in GitHub Desktop.
-- @module Mailer
-- requires 30log luarock install
local class = require '30log'
-- defaults with exchange server
local Mailer = class("Mailer",
{ server = "outlook.office365.com",
port = 993,
mailbox = "Inbox",
protocol = "tlsv1_2",
count = 0
})
function Mailer:init(username, password)
self.username, self.password = username, password
end
function Mailer:connect(type)
if type == "imap" then
-- require imap4 library
-- need to luarock install imap4 library
local imap4 = require "imap4"
-- setup connection
local connection = imap4(self.server, self.port, { protocol = self.protocol })
assert(connection:isCapable('IMAP4rev1'))
connection:login(self.username, self.password)
-- get information
local stat = connection:status(self.mailbox, { 'UNSEEN' })
-- close connection
connection:logout()
-- unseen email count
self.count = stat.UNSEEN
else
-- require pop3 library
-- need to luarock install pop3 library
local pop3 = require "pop3"
local connection = pop3:new()
-- open and authenticate
connection:open(self.server, self.port)
connection:auth(self.username, self.password)
-- get number of messages and sizes of each
count, _ = connection:stat()
-- email count
self.count = count
end
end
function Mailer:serverCapabilities()
-- require imap4 library
-- need to luarock install imap4 library
local imap4 = require "imap4"
-- setup connection
local connection = imap4(self.server, self.port, { protocol = self.protocol })
assert(connection:isCapable('IMAP4rev1'))
connection:login(self.username, self.password)
local capabilities = table.concat(connection:capability(), ', ')
-- close connection
connection:logout()
return capabilities
end
-- getter methods
function Mailer:getCount()
return self.count
end
-- setter methods
function Mailer:setServer(server)
self.server = server
end
function Mailer:setPort(port)
self.port = port
end
function Mailer:setMailBox(mailbox)
self.mailbox = mailbox
end
function Mailer:setprotocol(protocol)
self.protocol = protocol
end
return Mailer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment