Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | import sys |
| 3 | import smtplib |
| 4 | import argparse |
| 5 | import socket |
tylerliu | e0b60ac | 2020-09-15 21:37:23 -0700 | [diff] [blame^] | 6 | try: # python3 |
| 7 | from configparser import ConfigParser |
| 8 | except ImportError: # python2 |
| 9 | from ConfigParser import SafeConfigParser as ConfigParser |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 10 | from email.mime.multipart import MIMEMultipart |
| 11 | from email.mime.text import MIMEText |
| 12 | |
| 13 | socket.setdefaulttimeout(10) |
| 14 | |
| 15 | # init arg parser and parse |
| 16 | parser = argparse.ArgumentParser(description='Email-challenge-sender for NDNCERT') |
| 17 | parser.add_argument("email", help="the receiver email address") |
| 18 | parser.add_argument("secret", help="the secret of the challenge") |
| 19 | parser.add_argument("caName", help="the CA name") |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 20 | parser.add_argument("certName", help="the Ceritifcate being requested") |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 21 | args = parser.parse_args() |
| 22 | |
| 23 | # open config |
tylerliu | e0b60ac | 2020-09-15 21:37:23 -0700 | [diff] [blame^] | 24 | confParser = ConfigParser() |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 25 | confParser.read('@SYSCONFDIR@/ndncert/ndncert-mail.conf') |
| 26 | |
| 27 | # read smtp settings |
| 28 | encrypt_mode = confParser.get('ndncert_smtp_settings', "ENCRYPT_MODE") |
| 29 | server = confParser.get('ndncert_smtp_settings', 'SMTP_SERVER') |
| 30 | port = confParser.get('ndncert_smtp_settings', 'SMTP_PORT') |
| 31 | username = confParser.get('ndncert_smtp_settings', 'SMTP_USER') |
| 32 | password = confParser.get('ndncert_smtp_settings', 'SMTP_PASSWORD') |
| 33 | |
| 34 | # read email settings |
| 35 | msg_from = confParser.get('ndncert_email_settings', 'MAIL_FROM') |
| 36 | subject = confParser.get('ndncert_email_settings', 'SUBJECT') |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 37 | text = confParser.get('ndncert_email_settings', 'TEXT_TEMPLATE').format(args.secret, args.caName, args.certName) |
| 38 | html = confParser.get('ndncert_email_settings', 'HTML_TEMPLATE').format(args.secret, args.caName, args.certName) |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 39 | |
| 40 | # form email message |
| 41 | msg = MIMEMultipart('alternative') |
| 42 | msg.attach(MIMEText(text, 'plain')) |
| 43 | msg.attach(MIMEText(html, 'html')) |
| 44 | msg['From'] = msg_from |
| 45 | msg['To'] = args.email |
| 46 | msg['Subject'] = subject |
| 47 | |
| 48 | # send email |
| 49 | if encrypt_mode == 'ssl': |
| 50 | smtp_server = smtplib.SMTP_SSL(server, port) |
| 51 | else: # none or tls |
| 52 | smtp_server = smtplib.SMTP(server, port) |
| 53 | |
| 54 | if encrypt_mode != 'none': |
| 55 | smtp_server.ehlo() |
| 56 | if encrypt_mode == 'tls': |
| 57 | smtp_server.starttls() |
| 58 | |
| 59 | if username != '' and password != '': |
| 60 | smtp_server.login(username, password) |
| 61 | |
| 62 | smtp_server.sendmail(msg_from, args.email, msg.as_string()) |
| 63 | smtp_server.close() |
| 64 | sys.exit() |