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