jbe/bsw@0: local un = encode.mime.unstructured_header_line jbe/bsw@0: local mbl = encode.mime.mailbox_list_header_line jbe/bsw@0: jbe/bsw@0: local function encode_container(parts, container) jbe/bsw@0: if container.multipart then jbe/bsw@0: local boundary jbe/bsw@0: boundary = "BOUNDARY--" .. multirand.string( jbe/bsw@0: 24, jbe/bsw@0: "123456789BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz" jbe/bsw@0: ) jbe/bsw@0: parts[#parts+1] = "Content-Type: " jbe/bsw@0: parts[#parts+1] = "multipart/" jbe/bsw@0: parts[#parts+1] = container.multipart jbe/bsw@0: parts[#parts+1] = "; boundary=" jbe/bsw@0: parts[#parts+1] = boundary jbe/bsw@0: parts[#parts+1] = "\r\n\r\nMIME multipart\r\n" -- last \r\n optional jbe/bsw@0: for idx, sub_container in ipairs(container) do jbe/bsw@0: parts[#parts+1] = "\r\n--" jbe/bsw@0: parts[#parts+1] = boundary jbe/bsw@0: parts[#parts+1] = "\r\n" jbe/bsw@0: encode_container(parts, sub_container) jbe/bsw@0: end jbe/bsw@0: parts[#parts+1] = "\r\n--" jbe/bsw@0: parts[#parts+1] = boundary jbe/bsw@0: parts[#parts+1] = "--\r\n" jbe/bsw@0: else jbe/bsw@0: parts[#parts+1] = "Content-Type: " jbe/bsw@0: parts[#parts+1] = container.content_type or "text/plain" jbe/bsw@0: parts[#parts+1] = "\r\n" jbe/bsw@0: if container.content_id then jbe/bsw@0: parts[#parts+1] = "Content-ID: <" jbe/bsw@0: parts[#parts+1] = container.content_id jbe/bsw@0: parts[#parts+1] = ">\r\n" jbe/bsw@0: end jbe/bsw@0: if container.attachment_filename then jbe/bsw@0: parts[#parts+1] = "Content-Disposition: attachment; filename=" jbe/bsw@0: parts[#parts+1] = encode.mime.atom_token( jbe/bsw@0: container.attachment_filename jbe/bsw@0: ) jbe/bsw@0: parts[#parts+1] = "\r\n" jbe/bsw@0: end jbe/bsw@0: if container.binary then jbe/bsw@0: parts[#parts+1] = "Content-Transfer-Encoding: base64\r\n\r\n" jbe/bsw@0: parts[#parts+1] = encode.mime.base64(container.content) jbe/bsw@0: else jbe/bsw@0: parts[#parts+1] = jbe/bsw@0: "Content-Transfer-Encoding: quoted-printable\r\n\r\n" jbe/bsw@0: parts[#parts+1] = jbe/bsw@0: encode.mime.quoted_printable_text_content(container.content) jbe/bsw@0: end jbe/bsw@0: end jbe/bsw@0: end jbe/bsw@0: jbe/bsw@0: function encode.mime.mail(args) jbe/bsw@0: local parts = {} jbe/bsw@0: parts[#parts+1] = mbl("From", args.from) jbe/bsw@0: parts[#parts+1] = mbl("Sender", args.sender) jbe/bsw@0: parts[#parts+1] = mbl("Reply-To", args.reply_to) jbe/bsw@0: parts[#parts+1] = mbl("To", args.to) jbe/bsw@0: parts[#parts+1] = mbl("Cc", args.cc) jbe/bsw@0: parts[#parts+1] = mbl("Bcc", args.bcc) jbe/bsw@0: parts[#parts+1] = un("Subject", args.subject) jbe@558: if args.raw_headers then jbe@558: for i, raw_header in ipairs(args.raw_headers) do jbe@558: parts[#parts+1] = raw_header jbe@559: parts[#parts+1] = "\n" jbe@558: end jbe@558: end jbe/bsw@0: parts[#parts+1] = "MIME-Version: 1.0\r\n" jbe/bsw@0: encode_container(parts, args) jbe/bsw@0: return table.concat(parts) jbe/bsw@0: end