-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsv_mailer.py
More file actions
111 lines (96 loc) · 3.47 KB
/
csv_mailer.py
File metadata and controls
111 lines (96 loc) · 3.47 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
import smtplib
import csv
import time
from datetime import datetime, timedelta
# CONFIG
CSVFILE = "recipents.csv"
TEMPLATE = "mail_template.txt"
SENDER = '' # AGGIUNGI LA TUA EMAIL
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
USE_TLS = 1 # connessione criptata con SMTP server (1/si 0/no)
AUTH_REQUIRED = 1 # autorizzazione SMTP
SMTP_USER = '' # AGGIUNGI LA TUA EMAIL
SMTP_PASS = '' # AGGIUNGI LA TUA PASSWORD
# TEST
DRY_RUN = 0 # non invia mail ma stampa errori nel caso in cui ce ne sarebbero potuti essere
SAFE_MODE = 0 # invia mail al recipients al posto di csv
RECIPIENTS = ['alberto.tomasin@gmail.com']
template = open(TEMPLATE, encoding='utf-8')
csvfile = open(CSVFILE, "r")
mail_template = template.read()
csv_reader = csv.DictReader(csvfile) # add 'dialect="semicolon"' if necessary
lines = len(list(csv_reader))
now = datetime.today()
print("Ora attuale: "+ str(now))
result_2 = now + timedelta(seconds=(lines * 1.8 + (lines/50*120)))
print("Ora stimata di fine esecuzione: " + str(result_2))
csvfile = open(CSVFILE, "r")
csv_reader = csv.DictReader(csvfile) # add 'dialect="semicolon"' if necessary
# Inizia sessione SMTP
smtpresult = 0
if not DRY_RUN:
print("Sessione SMTP aperta")
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
# session.set_debuglevel(1)
session.ehlo()
if USE_TLS and session.has_extn("STARTTLS"): # not tested!
session.starttls()
session.ehlo()
if AUTH_REQUIRED:
session.login(SMTP_USER, SMTP_PASS)
counter = 1
# Invio mail
for row in csv_reader:
if counter % 50 == 0:
print("Chiudo sessione SMTP")
session.close()
print("E' tempo di dormire")
time.sleep(120);
print("Riapro la sessione SMTP")
if not DRY_RUN:
print("Sessione SMTP aperta")
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
# session.set_debuglevel(1)
session.ehlo()
if USE_TLS and session.has_extn("STARTTLS"): # not tested!
session.starttls()
session.ehlo()
if AUTH_REQUIRED:
session.login(SMTP_USER, SMTP_PASS)
counter += 1
# prendo dati da csv
surname = "" # AGGIUNGI IL TUO NOME
givenname = "" # AGGIUNGI IL TUO COGNOME
email = row["email"]
id = "Questionario yeswork - lavoro in un tocco"
recipient = "\"" + givenname + " " + surname + "\" " + "<" + email + ">"
time.sleep(1)
print(str(counter) + " - Invio mail a " + email)
mssg = mail_template.replace("$NAME$", givenname)
#mssg = mssg.replace("$SENDER$", SENDER)
#mssg = mssg.replace("$RECIPIENT$", recipient)
#mssg = mssg.replace("$EMAIL$", email)
mssg = mssg.replace("$ID$", id)
if SAFE_MODE:
recipients = RECIPIENTS
print("[SAFE MODE] Invio mail a " + recipients[0])
mssg = mssg + "\r\nquesto mess sarebbe dovuto andare a: " + email
else:
recipients = [email]
if DRY_RUN:
print("[DRY RUN] Invio mail a " + recipients[0])
print()
print(mssg)
print()
print("################################################################################")
else:
smtpresult = session.sendmail(SENDER, recipients, mssg.encode('UTF-8'))
if smtpresult:
errstr = ""
for recip in smtpresult.keys():
errstr = """Errore nell'invio della mail a: %s
L'errore e': %s
%s
%s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
raise smtplib.SMTPException(errstr)