Last active
July 13, 2018 22:20
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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