-
Notifications
You must be signed in to change notification settings - Fork 3
/
sending_gmail.py
78 lines (59 loc) · 2.24 KB
/
sending_gmail.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
76
77
78
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 4 14:09:32 2020
@author: Mohammad sina Allahkaram
"""
import argparse
import getpass
import pandas as pd
import os
from pyautomail import EmailSender
from pyautomail.utils import init_logger
def arg_parser():
parser = argparse.ArgumentParser()
parser.add_argument("email", help="enter you Gmail account")
parser.add_argument("contacts", help="path to csv file which contact emails are listed there")
group = parser.add_mutually_exclusive_group()
group.add_argument("--subject_txt", help="string of subject")
group.add_argument("--subject_file", help="text file that 'subject' written there")
parser.add_argument("--body", help="path to E-mail text file")
parser.add_argument("--html", help="path to E-mail Html file")
parser.add_argument("--attachment", help="path to pdf file that you want to attach to your E-mail")
parser.add_argument("--cpdf", action='store_true', help="custom pdf file that you want to attach to your E-mail",
default=False)
parser.add_argument("--pdf_dir", default='')
parser.add_argument("--template", help="path to template file")
return parser.parse_args()
logger = init_logger('main')
logger.info("Reading arguments...")
arg = arg_parser()
sender_email = arg.email
contact_df = pd.read_csv(arg.contacts)
password = getpass.getpass("Enter you email password :")
# Create a secure SSL context
logger.info("Creating EmailSender obj...")
sender = EmailSender(cfg="config.cfg", user=sender_email, password=password)
subject = ''
if arg.subject_txt:
subject = arg.subject_txt
elif arg.subject_file:
with open(arg.subject_file) as f:
subject = f.read()
logger.info("Subject: {}".format(subject))
for i in contact_df.iterrows():
if arg.body:
sender.set_template(arg.body)
if arg.html:
sender.set_template(arg.html)
if arg.template:
sender.set_template(arg.template)
filename = None
if arg.attachment:
filename = arg.attachment
if arg.cpdf:
filename = os.path.join(arg.pdf_dir, str(i[1]['cpdf']) + '.pdf')
data = {}
for item in i[1].items():
data[item[0]] = item[1]
print(data)
sender.send(i[1]['email'], subject, data, filename)