Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


Flask Sendmail

06 Jul 2020 » programming

Bash sendmail

subject="subject"
body=$(cat ./lol)
curl -XPOST --data-urlencode "payload={\"subject\": \"$subject\", \"msg\": \"$body\"}" <flask endpoint> 
subject="subject"
body=$(cat ./lol | base64)
curl -XPOST --data-urlencode "payload={\"isbase64\": 1, \"subject\": \"$subject\", \"msg\": \"$body\"}" <flask endpoint> 
req = request.form.get("payload")
req = json.loads(req, strict=False)
toil.phead(req)
subject = req.get("subject")
msg = req.get("msg")
if "isbase64" in req:
    msg = base64.b64decode(msg).decode()
sendmail(subject, msg)


def sendmail(subject='Subject', msg='body', sender='default-sender', receivers='default-recv'):
    import smtplib

    message = f"From: From Root <{sender}>\nTo: <{receivers}>\nSubject: {subject}\n\n{msg}"

    try:
       smtpObj = smtplib.SMTP('localhost')
       smtpObj.sendmail(sender, receivers, message)
       print("Successfully sent email")
    except SMTPException:
       print("Error: unable to send email")