Created
July 22, 2015 03:17
-
-
Save dnozay/5d43f3580ea2318a4041 to your computer and use it in GitHub Desktop.
test email script (using python)
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 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