blob: 1916791e184c6327ac2aae4791be66d2b3660796 [file] [log] [blame]
Zhiyi Zhang576aad12017-10-03 15:41:53 -07001#!/usr/bin/env python
2import sys
3import smtplib
4import argparse
5import socket
6from ConfigParser import SafeConfigParser
7from 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")
17args = parser.parse_args()
18
19# open config
20confParser = SafeConfigParser()
21confParser.read('@SYSCONFDIR@/ndncert/ndncert-mail.conf')
22
23# read smtp settings
24encrypt_mode = confParser.get('ndncert_smtp_settings', "ENCRYPT_MODE")
25server = confParser.get('ndncert_smtp_settings', 'SMTP_SERVER')
26port = confParser.get('ndncert_smtp_settings', 'SMTP_PORT')
27username = confParser.get('ndncert_smtp_settings', 'SMTP_USER')
28password = confParser.get('ndncert_smtp_settings', 'SMTP_PASSWORD')
29
30# read email settings
31msg_from = confParser.get('ndncert_email_settings', 'MAIL_FROM')
32subject = confParser.get('ndncert_email_settings', 'SUBJECT')
33text = confParser.get('ndncert_email_settings', 'TEXT_TEMPLATE').format(args.secret, args.caName)
34html = confParser.get('ndncert_email_settings', 'HTML_TEMPLATE').format(args.secret, args.caName)
35
36# form email message
37msg = MIMEMultipart('alternative')
38msg.attach(MIMEText(text, 'plain'))
39msg.attach(MIMEText(html, 'html'))
40msg['From'] = msg_from
41msg['To'] = args.email
42msg['Subject'] = subject
43
44# send email
45if encrypt_mode == 'ssl':
46 smtp_server = smtplib.SMTP_SSL(server, port)
47else: # none or tls
48 smtp_server = smtplib.SMTP(server, port)
49
50if encrypt_mode != 'none':
51 smtp_server.ehlo()
52 if encrypt_mode == 'tls':
53 smtp_server.starttls()
54
55if username != '' and password != '':
56 smtp_server.login(username, password)
57
58smtp_server.sendmail(msg_from, args.email, msg.as_string())
59smtp_server.close()
60sys.exit()