send-email-challenge: modernize the code a bit
Change-Id: Ifb887eeef2ca8ccca2914486f2b9d002d3654534
diff --git a/ndncert-mail.conf.sample b/ndncert-mail.conf.sample
index a33e458..48e31b1 100644
--- a/ndncert-mail.conf.sample
+++ b/ndncert-mail.conf.sample
@@ -1,12 +1,12 @@
-[ndncert_smtp_settings]
+[ndncert.smtp]
SMTP_SERVER = localhost or remote smtp server
SMTP_PORT = port number, usually one from 25 465 587
ENCRYPT_MODE = select one from ssl/tls/none
SMTP_USER = leave it empty if you do not have one
SMTP_PASSWORD = leave it empty if you do not have one
-[ndncert_email_settings]
+[ndncert.email]
MAIL_FROM = NDN Testbed Certificate Robot <noreply-ndncert@named-data.net>
SUBJECT = Email Challenge Triggered by NDNCERT
-TEXT_TEMPLATE = Your PIN code: {0} from NDNCERT CA {1}. Certificate Name: {2}. Your email has been used to apply for a digital certificate from NDNCERT. Please keep it secret and type in to your application to finish the certificiate issuance process. If you do not know what is going on, please ignore the message.
-HTML_TEMPLATE = <html><head></head><body><p><b>Your PIN code: {0} from NDNCERT CA {1}. Certificate Name: {2}.</b></p><p>Your email has been used to apply for a digital certificate from NDNCERT. Please keep it secret and type in to your application to finish the certificiate issuance process. If you do not know what is going on, please ignore the message.</p><p>Sincerely,<br/>NDN Testbed NDNCERT robot</p>
\ No newline at end of file
+TEXT_TEMPLATE = Your PIN code: {0} from NDNCERT CA {1}. Certificate Name: {2}. Your email has been used to apply for a digital certificate from NDNCERT. Please keep it secret and type it in your application to complete the certificate issuance process. If you do not know what is going on, please ignore this message.
+HTML_TEMPLATE = <html><head></head><body><p><b>Your PIN code: {0} from NDNCERT CA {1}. Certificate Name: {2}.</b></p><p>Your email has been used to apply for a digital certificate from NDNCERT. Please keep it secret and type it in your application to complete the certificate issuance process. If you do not know what is going on, please ignore this message.</p><p>Sincerely,<br/>NDN Testbed NDNCERT robot</p>
diff --git a/ndncert-send-email-challenge.py b/ndncert-send-email-challenge.py
index 20198fc..56d8bbe 100755
--- a/ndncert-send-email-challenge.py
+++ b/ndncert-send-email-challenge.py
@@ -1,61 +1,55 @@
#!/usr/bin/env python3
-import sys
-import smtplib
import argparse
-import socket
-from configparser import ConfigParser
-from email.mime.multipart import MIMEMultipart
-from email.mime.text import MIMEText
-
-socket.setdefaulttimeout(10)
+import configparser
+import smtplib
+from email.message import EmailMessage
# init arg parser and parse
-parser = argparse.ArgumentParser(description='Email-challenge-sender for NDNCERT')
-parser.add_argument("email", help="the receiver email address")
-parser.add_argument("secret", help="the secret of the challenge")
-parser.add_argument("caName", help="the CA name")
-parser.add_argument("certName", help="the Ceritifcate being requested")
+parser = argparse.ArgumentParser(description='Email challenge sender for NDNCERT CA')
+parser.add_argument('email', help='email address of the recipient')
+parser.add_argument('secret', help='secret code for the challenge')
+parser.add_argument('ca_name', help='name of the certificate authority')
+parser.add_argument('cert_name', help='name of the certificate being requested')
args = parser.parse_args()
-# open config
-confParser = ConfigParser()
+# open config file
+confParser = configparser.ConfigParser()
confParser.read('@SYSCONFDIR@/ndncert/ndncert-mail.conf')
# read smtp settings
-encrypt_mode = confParser.get('ndncert_smtp_settings', "ENCRYPT_MODE")
-server = confParser.get('ndncert_smtp_settings', 'SMTP_SERVER')
-port = confParser.get('ndncert_smtp_settings', 'SMTP_PORT')
-username = confParser.get('ndncert_smtp_settings', 'SMTP_USER')
-password = confParser.get('ndncert_smtp_settings', 'SMTP_PASSWORD')
+encrypt_mode = confParser.get('ndncert.smtp', 'encrypt_mode')
+server = confParser.get('ndncert.smtp', 'smtp_server')
+port = confParser.get('ndncert.smtp', 'smtp_port')
+username = confParser.get('ndncert.smtp', 'smtp_user')
+password = confParser.get('ndncert.smtp', 'smtp_password')
# read email settings
-msg_from = confParser.get('ndncert_email_settings', 'MAIL_FROM')
-subject = confParser.get('ndncert_email_settings', 'SUBJECT')
-text = confParser.get('ndncert_email_settings', 'TEXT_TEMPLATE').format(args.secret, args.caName, args.certName)
-html = confParser.get('ndncert_email_settings', 'HTML_TEMPLATE').format(args.secret, args.caName, args.certName)
+from_addr = confParser.get('ndncert.email', 'mail_from')
+subject = confParser.get('ndncert.email', 'subject')
+text = confParser.get('ndncert.email', 'text_template').format(args.secret, args.ca_name, args.cert_name)
+html = confParser.get('ndncert.email', 'html_template').format(args.secret, args.ca_name, args.cert_name)
-# form email message
-msg = MIMEMultipart('alternative')
-msg.attach(MIMEText(text, 'plain'))
-msg.attach(MIMEText(html, 'html'))
-msg['From'] = msg_from
+# create email message
+msg = EmailMessage()
+msg['From'] = from_addr
msg['To'] = args.email
msg['Subject'] = subject
+msg.set_content(text)
+msg.add_alternative(html, subtype='html')
-# send email
+# connect to SMTP server
if encrypt_mode == 'ssl':
- smtp_server = smtplib.SMTP_SSL(server, port)
-else: # none or tls
- smtp_server = smtplib.SMTP(server, port)
+ context = smtplib.SMTP_SSL(server, port, timeout=10)
+elif encrypt_mode == 'tls':
+ context = smtplib.SMTP(server, port, timeout=10)
+ context.starttls()
+elif encrypt_mode == 'none':
+ context = smtplib.SMTP(server, port, timeout=10)
+else:
+ raise ValueError(f'Invalid encrypt_mode: {encrypt_mode}')
-if encrypt_mode != 'none':
- smtp_server.ehlo()
- if encrypt_mode == 'tls':
- smtp_server.starttls()
-
-if username != '' and password != '':
- smtp_server.login(username, password)
-
-smtp_server.sendmail(msg_from, args.email, msg.as_string())
-smtp_server.close()
-sys.exit()
+with context as smtp:
+ if username and password:
+ smtp.login(username, password)
+ # send email
+ smtp.send_message(msg)