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