webmcp
view framework/env/net/send_mail.lua @ 566:3b71fdb3a00d
Updated year + version in documentation
| author | jbe | 
|---|---|
| date | Wed Apr 28 12:54:38 2021 +0200 (2021-04-28) | 
| parents | b7596f5158d4 | 
| children | 
 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   raw_headers   = raw_headers,     -- sequence of raw header lines
    13   multipart     = multipart_type,  -- "alternative", "mixed", "related", or nil
    14   content_type  = content_type,    -- only for multipart == nil, defaults to "text/plain"
    15   binary        = binary,          -- allow full 8-bit content
    16   content       = content or {     -- content as lua-string, or table in case of multipart
    17     {
    18       multipart = multipart_type,
    19       ...,
    20       content   = content or {
    21         {...}, ...
    22       }
    23     }, {
    24       ...
    25     },
    26     ...
    27   }
    28 }
    30 This function sends a mail using the /usr/sbin/sendmail command. It returns true on success, otherwise false.
    32 --]]--
    34 function net.send_mail(args)
    35   local mail
    36   if type(args) == "string" then
    37     mail = args
    38   else
    39     mail = encode.mime.mail(args)
    40   end
    41   local envelope_from = args.envelope_from
    42   local command = table.new(net._mail_config.command)
    43   if
    44     envelope_from and
    45     net._mail_config.envelope_from_option and
    46     string.find(envelope_from, "^[0-9A-Za-z%.-_@0-9A-Za-z%.-_]+$")
    47   then
    48     command[#command+1] = net._mail_config.envelope_from_option
    49     command[#command+1] = envelope_from
    50   end
    51   local stdout, errmsg, status = extos.pfilter(mail, table.unpack(command))
    52   if not status then
    53     error("Error while calling sendmail: " .. errmsg)
    54   end
    55   if status == 0 then
    56     return true
    57   else
    58     return false
    59   end
    60 end
