Skip to content

Instantly share code, notes, and snippets.

@dnozay
Created July 22, 2015 03:17
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 dnozay/5d43f3580ea2318a4041 to your computer and use it in GitHub Desktop.
Save dnozay/5d43f3580ea2318a4041 to your computer and use it in GitHub Desktop.
test email script (using python)
#!/usr/bin/env python
# simple script to test that email (internal relay) is working.
import smtplib
import argparse
from uuid import uuid4
from email.mime.text import MIMEText
def smtp_mail(subject, message, addr_from, addr_to, smtp_host):
msg = MIMEText(message.encode('utf-8'), 'plain', 'utf-8')
msg['Subject'] = subject.encode('utf-8')
msg['From'] = addr_from
if isinstance(addr_to, (list, tuple)):
msg['To'] = ",".join(addr_to)
else:
msg['To'] = addr_to
addr_to = [addr_to]
s = smtplib.SMTP(smtp_host)
s.sendmail(addr_from, addr_to, msg.as_string())
s.quit()
def test_email(addr_from, addr_to, smtp_host):
subject = 'test email'
message = 'test email %s' % uuid4()
smtp_mail(subject, message, addr_from, addr_to, smtp_host)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('smtp_host')
parser.add_argument('addr_from')
parser.add_argument('addr_to')
args = parser.parse_args()
test_email(args.addr_from, args.addr_to, args.smtp_host)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment