webmcp

view framework/env/net/send_mail.lua @ 64:3d43a5cf17c1

Compatibility with Lua 5.2
author jbe
date Sun Apr 15 16:04:33 2012 +0200 (2012-04-15)
parents 4f9f7fef80ee
children a54cc7dcabf1
line source
1 --[[--
2 success = -- true, if mail has been sent successfully, otherwise false
3 net.send_mail{
4 envelope_from = envelope_from, -- envelope from address, not part of mail headers
5 from = from, -- From header address or table with 'name' and 'address' fields
6 sender = sender, -- Sender header address or table with 'name' and 'address' fields
7 reply_to = reply_to, -- Reply-To header address or table with 'name' and 'address' fields
8 to = to, -- To header address or table with 'name' and 'address' fields
9 cc = cc, -- Cc header address or table with 'name' and 'address' fields
10 bcc = bcc, -- Bcc header address or table with 'name' and 'address' fields
11 subject = subject, -- subject of e-mail
12 multipart = multipart_type, -- "alternative", "mixed", "related", or nil
13 content_type = content_type, -- only for multipart == nil, defaults to "text/plain"
14 binary = binary, -- allow full 8-bit content
15 content = content or { -- content as lua-string, or table in case of multipart
16 {
17 multipart = multipart_type,
18 ...,
19 content = content or {
20 {...}, ...
21 }
22 }, {
23 ...
24 },
25 ...
26 }
27 }
29 This function sends a mail using the /usr/sbin/sendmail command. It returns true on success, otherwise false.
31 --]]--
33 function net.send_mail(args)
34 local mail
35 if type(args) == "string" then
36 mail = args
37 else
38 mail = encode.mime.mail(args)
39 end
40 local envelope_from = args.envelope_from
41 local command = {"/usr/sbin/sendmail", "-t", "-i"}
42 if
43 envelope_from and
44 string.find(envelope_from, "^[0-9A-Za-z%.-_@0-9A-Za-z%.-_]+$")
45 then
46 command[#command+1] = "-f"
47 command[#command+1] = envelope_from
48 end
49 local stdout, errmsg, status = os.pfilter(mail, table.unpack(command))
50 if not status then
51 error("Error while calling sendmail: " .. errmsg)
52 end
53 if status == 0 then
54 return true
55 else
56 return false
57 end
58 end

Impressum / About Us