-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailsend.py
More file actions
172 lines (150 loc) · 6.69 KB
/
Copy pathmailsend.py
File metadata and controls
172 lines (150 loc) · 6.69 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from typing import Generator
import smtplib
import sys
from email import encoders
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from string import Template
from time import sleep
import keyring
import pandas as pd
import click
click.clear()
service_id = 'MAILSEND'
click.echo(click.style('''
_ __ __
____ ___ ____ _(_) /_______ ____ ____/ /
/ __ `__ \/ __ `/ / / ___/ _ \/ __ \/ __ /
/ / / / / / /_/ / / (__ ) __/ / / / /_/ /
/_/ /_/ /_/\__,_/_/_/____/\___/_/ /_/\__,_/
''', fg='green'))
click.echo('welcome to mailsend!')
click.echo('written by Devansh Joshi')
click.echo("you may need google app passwords to run this script read about them here- https://support.google.com/accounts/answer/185833?hl=en")
click.echo()
def extract_names(temp: Template, in_cols: list) -> set:
found = set()
for match in Template.pattern.finditer(temp.template):
name = match.group('named') or match.group('braced')
if name is not None and name in in_cols:
found.add(name)
return found
def extract_file_data(fname: str) -> tuple:
df = pd.read_csv(fname)
cols = set(df.columns.values)
return df, cols
def format_text(msg: str, df: pd.DataFrame, add_file: str, in_cols: list) -> Generator[tuple, None, None]:
msg = Template(msg)
var_dict = dict()
with open(add_file, 'r') as afile:
add_list = [add.strip() for add in afile.readlines()]
names = extract_names(msg, in_cols)
for i, address in enumerate(add_list):
for name in names:
var_dict[name] = df[name][i]
yield address, msg.safe_substitute(var_dict)
def create_mail(msg: str, sender: str, target: str, sub: str, attach: str) -> str:
message = MIMEMultipart()
message['From'] = sender
message['To'] = target
message['Subject'] = sub
message.attach(MIMEText(msg, 'plain'))
if attach:
at = MIMEBase('application', 'octate-stream')
with open(attach, 'r') as atfile:
at.set_payload(atfile.read())
encoders.encode_base64(at)
at.add_header('Content-Disposition', f'attachment; filename={attach}')
message.attach(at)
return message.as_string()
def write_mail(fname: str, add_file: str, email_id: str, passwd: str, attach: str) -> None:
srv = smtplib.SMTP('smtp.gmail.com', 587)
confirm = True
try:
click.clear()
click.echo('please enter the message to be sent \n')
msg = sys.stdin.readlines()
msg = ''.join([line for line in msg])
sub = click.prompt('enter subject for email \n', prompt_suffix='>> ')
srv.starttls()
srv.login(email_id, passwd)
if fname:
df, cols = extract_file_data(fname)
for address, body in format_text(msg, df, add_file, cols):
if confirm:
click.echo(body)
if click.confirm('are you sure you want to proceed with this email?'):
confirm = False
else:
sys.exit('Aborted!')
srv.sendmail(from_addr=email_id, to_addrs=address, msg=create_mail(
msg=body, sender=email_id, target=address, sub=sub, attach=attach))
sleep(1.0)
else:
with open(add_file, 'r') as afile:
add_list = [add.strip() for add in afile.readlines()]
for address in add_list:
if confirm:
click.echo(msg)
if click.confirm('are you sure you want to proceed with this email?'):
confirm = False
click.clear()
else:
sys.exit('Aborted!')
srv.sendmail(from_addr=email_id, to_addrs=address, msg=create_mail(
msg=msg, sender=email_id, target=address, sub=sub, attach=attach))
finally:
srv.quit()
def get_credentials() -> tuple:
while True:
email_id = click.prompt('enter your email id \n', prompt_suffix='>> ')
passwd = keyring.get_password(service_id, email_id)
if passwd:
res = click.prompt(
'type del to delete this email anything else to continue \n', prompt_suffix='>> ', default='', show_default=False)
if res.lower() == 'del':
keyring.delete_password(service_id, email_id)
click.echo('deleted!')
continue
return email_id, passwd
else:
passwd = click.prompt(
'new email? enter password \n', prompt_suffix='>> ')
keyring.set_password(service_id, email_id, passwd)
return email_id, passwd
@click.command()
@click.argument('target_file', type=click.Path(exists=True))
@click.option('-a', '--attach', type=click.Path(exists=True), help='path to attachment file')
@click.option('-f', '--fname', type=click.Path(exists=True), help='path to the csv file')
def cli_face(target_file, attach, fname) -> None:
''' This script intends to automate mailing through gmail, just type the name of the script followed by the
file that contains the list of addresses, this file is supposed to be a plain text file containing a single
email address on each line. There is a also an option to add variables to your email body, just specify a csv
file with all the data that you want to use and type $column_name in the body to substitute the value of the column.
attachments are supported as well.'''
try:
if fname:
email_id, passwd = get_credentials()
write_mail(fname=fname, add_file=target_file,
email_id=email_id, passwd=passwd, attach=attach)
sys.exit('mailed succesfully!')
else:
if click.confirm('are you sure you want to send a regular email?'):
email_id, passwd = get_credentials()
write_mail(fname=fname, add_file=target_file,
email_id=email_id, passwd=passwd, attach=attach)
sys.exit('mailed successfully!')
else:
sys.exit('Aborted!')
except smtplib.SMTPAuthenticationError:
click.echo('Please check the username and password entered!')
click.echo(
'... or if you have 2 factor authentication turned on then you need to use an app password instead of your regular one.')
keyring.delete_password(service_id, email_id)
sys.exit()
except TypeError:
click.echo(
'one or both of the files seem to be the wrong type, please check them again!')
sys.exit()
cli_face()