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