include("import") func mk_pack_attach() if (not isset(has_pack_attach)) gvar(has_pack_attach) fvar(old) old = block.select(BLK_FUNC) println('# Based on Python example for email package') println('# See http://docs.python.org/library/email-examples.html#id2') println("def pack_attach(self,filename):" block.inclvl() println("ctype, encoding = mimetypes.guess_type(", filename, ")") println("if ctype is None or encoding is not None:") println("# No guess could be made, or the file is encoded (compressed), so") println("# use a generic bag-of-bits type.") block.inclvl() println("ctype = 'application/octet-stream'") println("maintype, subtype = ctype.split('/', 1)") block.declvl() println("if maintype == 'text':") block.inclvl() println("fp = open(", filename, ")") println("# Note: we should handle calculating the charset") println("msg = email.mime.text.MIMEText(fp.read(), _subtype=subtype)") println("fp.close()") block.declvl() println("elif maintype == 'image':") block.inclvl() println("fp = open(path, 'rb')") println("msg = email.mime.image.MIMEImage(fp.read(), _subtype=subtype)") println("fp.close()") block.declvl() println("elif maintype == 'audio':") block.inclvl() println("fp = open(path, 'rb')") println("msg = email.mime.audio.MIMEAudio(fp.read(), _subtype=subtype)") println("fp.close()") block.declvl() println("else:") block.inclvl() println("fp = open(path, 'rb')") println("msg = email.mime.base.MIMEBase(maintype, subtype)") println("msg.set_payload(fp.read())") println("fp.close()") block.declvl() println("# Encode the payload using Base64") println("email.encoders.encode_base64(msg)") println("# Set the filename parameter") println("msg.add_header('Content-Disposition', 'attachment', filename=filename)") println("return msg") block.declvl() block.select(old) end end func doSend(_data) fvar(type, att, enc, usr, pass, key, cert, f, t, keycert, portx) att = Attach enc = Encryption usr = Login pass = Password key = KeyFile cert = CertFile f = From if (expof(t) == PyList) t = '", ".join(' && To && ')' else t = To if (isndef(Port)) portx = ','&&Port else portx = '' end need_smtplib() need_email() need_mimetypes() if (not isset(att)) println(msg, ' = MIMEText(', Body, ')') type = 0 else println(msg, ' = MIMEMultipart()') type = 1 end println(msg, '["From"] = ', f) println(msg, '["To"] = ', t) println(msg, '["Subject"] = ', Subject) if (type == 1) println('for ', filename, 'in ', att, ':') block.inclvl() println(msg, '.attach(pack_attach(', filename, '))') block.declvl() println(msg, '.attach(MIMEText(', Body, '))') end println(composed, ' = ', msg, '.as_string()') keycert = '' if (isdef(key)) keycert = key if (isdef(cert)) keycert = keycert && ',' && cert end end if (enc == 0) //plain println(conn, ' = smtplib.SMTP(', Host, portx, ')') elseif (enc == 1) //STARTTLS println(conn, ' = smtplib.SMTP(', Host, portx, ')') println(conn, '.starttls(', keycert, ')') elseif (enc == 2) //SSL/TLS println(conn, ' = smtplib.SMTP_SSL(', Host, portx, keycert,')') else error("SMTP: Unknown encryption!") end if (isndef(usr)) println(conn, '.login(', usr, ',', pass, ')') end println(res, ' = ', conn, '.sendmail(', f, ',', t, ',', composed, ')') println(conn, '.quit()') event(onSend, res) end