Skip to content

Instantly share code, notes, and snippets.

@craigphicks
Last active July 13, 2018 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigphicks/703087a1addb1f67dbfa418f6b170567 to your computer and use it in GitHub Desktop.
Save craigphicks/703087a1addb1f67dbfa418f6b170567 to your computer and use it in GitHub Desktop.
python 40 line drop-in replacement for sendmail/postfix etc - avoid setting files when only using mail to send system notifications to fixed address
#!/usr/bin/env python3
import os
import sys
import subprocess
s = ""
s += "---- begin args ----\n"
for i in range(0,len( sys.argv )):
s+= "argv[" + str(i) + "]:" + str(sys.argv[i]) +"\n"
s += "---- end args----\n"
s += "---- begin message body ----\n"
for line in sys.stdin:
s+=line
s += "---- end message body ----\n"
cmdlist = [
'/usr/bin/curl',
'-s',
# '--trace-ascii', '/tmp/mg1.log',
'--user', 'api:key-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'https://api.mailgun.net/v3/example.com/messages',
'-F', 'to=user@gmail.com',
'-F', 'from=sendmail@example.com',
'-F', 'subject=sendmail',
'-F', 'text=<-'
]
p=subprocess.run(cmdlist, input=s.encode(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
print("return code: {}\nstdout:\n{}\nstderr:\n{}\n".format(
p.returncode, p.stdout, p.stderr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment