-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendMails.py
75 lines (72 loc) · 2.56 KB
/
sendMails.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import json
import pandas as pd
import smtplib, ssl
from unidecode import unidecode
import click
from sys import stderr
from time import sleep
@click.command()
@click.option("--port", '-p',
type=int,
default=587,
help="SMTP port to use")
@click.option("--smtp-server", '-s',
type=str,
default="smtp.gmail.com",
help="SMTP server to use")
@click.option("--sender-email", '-e',
type=str,
default="international.club.ieseg@gmail.com",
help="email adress to use")
@click.option("--copy-to", '-c',
type=str,
multiple=True,
help="E-mail addresses to put in copy of sent mails")
@click.option("--matches-file", '-m',
type=click.File(),
default="./output/matchings.csv",
help="Input file containing the matches")
@click.option("--config-file", '-conf',
type=click.File(),
default="./config.json",
help="JSON config file")
@click.option("--pwd-file", '-p',
type=click.File(),
help="File containing the SMTP password. \
If not provided, will prompt for password")
@click.option("--dry", '-d',
is_flag=True,
help="Do not actually send emails")
@click.option("--verbose", '-v',
count=True,
help="Show more info in stderror")
def send_mails(port,
smtp_server,
sender_email,
copy_to,
matches_file,
config_file,
pwd_file,
dry,
verbose):
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.starttls(context=context)
if not pwd_file:
pwd = input("Enter your e-mail password : ")
else:
pwd = pwd_file.read().strip()
server.login(sender_email, pwd)
def send(body, dests):
fullBody = "To:" + ','.join(dests) + '\n' + body
server.sendmail(sender_email, dests, unidecode(fullBody))
matches = pd.read_csv(matches_file)
config = json.load(config_file)
for i, m in matches.iterrows():
sleep(1)
if verbose > 0:
print("Sending to ", m["exEMail"], "and", m["frEMail"],
file=stderr)
if not dry:
send(config["mails"][m["language"]].format(**m.to_dict()),
[m["exEMail"], m["frEMail"]] + list(copy_to))