in site/api/compose.lua [28:144]
function handle(r)
local account = user.get(r)
cross.contentType(r, "application/json")
if account and account.cid then
local post = r:parsebody(1024*1024)
if post.to and post.subject and post.body then
local to = ("<%s>"):format(post.to)
local fp, lp = post.to:match("([^@]+)@([^@]+)")
local domainIsOkay = false
if type(config.accepted_domains) == "string" then
if r.strcmp_match(lp, config.accepted_domains) or config.accepted_domains == "*" then
domainIsOkay = true
end
elseif type(config.accepted_domains) == "table" then
for k, ad in pairs(config.accepted_domains) do
if r.strcmp_match(lp, ad) or ad == "*" then
domainIsOkay = true
break
end
end
end
if domainIsOkay then
local fname = nil
if account.preferences then
fname = account.preferences.fullname
end
local fr = ([[%s<%s>]]):format(fname or account.credentials.fullname, account.credentials.email)
if account.credentials.altemail and post.alt then
for k, v in pairs(account.credentials.altemail) do
if v.email == post.alt then
fr = ([[%s<%s>]]):format(fname or account.credentials.fullname, v.email)
break
end
end
end
local headers = {
['Content-Type'] = 'text/plain; charset=utf-8',
['X-PonyMail-Sender'] = r:sha1(account.cid),
['X-PonyMail-Agent'] = "PonyMail Composer/0.2",
['message-id'] = ("<pony-%s-%s@%s>"):format(r:sha1(account.cid), r:sha1(r:clock() .. os.time() .. r.useragent_ip), post.to:gsub("@", ".")),
to = to,
subject = post.subject,
from = fr,
}
if post['references'] then
headers['references'] = post['references']
end
if post['in-reply-to'] then
headers['in-reply-to'] = post['in-reply-to']
end
local msgbody = post.body
if config.email_footer then
local subs = {
list = to:gsub("[<>]", ""),
domain = r.hostname,
port = r.port,
msgid = headers['message-id']
}
msgbody = msgbody .. "\n" .. config.email_footer:gsub("$([a-z]+)", function(a) return subs[a] or a end)
end
local source = smtp.message{
headers = headers,
body = msgbody
}
local rv, er = smtp.send{
from = fr:gsub(".-<", "<"),
rcpt = to,
source = source,
server = config.mailserver
}
r:puts(JSON.encode{
result = rv,
error = er,
src = headers
})
else
r:puts(JSON.encode{
error = "Invalid recipient specified."
})
end
else
r:puts(JSON.encode{
error = "Invalid or missing headers",
headers = post
})
end
else
r:puts[[{"error": "You need to be logged in before you can send emails"}]]
end
return cross.OK
end