-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-emailer.py
85 lines (75 loc) · 2.62 KB
/
auto-emailer.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
79
80
81
82
83
84
85
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from enum import Enum
import smtplib
from dotenv import load_dotenv
import os
import csv
# Custom variables (change these)
file_path = 'docs/contacts.csv'
name_col = 0
recipient_col = 1
language_col = 3
email_subject = 'Test email'
# Load environment variables
load_dotenv()
EMAIL_ADDRESS = os.getenv('EMAIL_ADDRESS')
EMAIL_PASSWORD = os.getenv('EMAIL_PASSWORD')
# Definitions
class Language(Enum):
CATALAN = 'CA'
SPANISH = 'ES'
def read_csv(file_path):
with open(file_path, mode='r', newline='') as file:
return list(csv.reader(file))
def read_txt(file_path):
with open(file_path, 'r') as file:
return file.read()
def send_email(subject, body, recipient, attachments=[]):
msg = MIMEMultipart()
msg['From'] = EMAIL_ADDRESS
msg['To'] = recipient
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
for attachment in attachments:
try:
with open(attachment, 'rb') as file:
part = MIMEBase('application', 'octet-stream')
part.set_payload(file.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f'attachment; filename={os.path.basename(attachment)}')
msg.attach(part)
except Exception as e:
print(f'Failed to attach file {attachment}: {e}')
try:
with smtplib.SMTP('smtp.gmail.com', 587) as server:
server.starttls()
server.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
server.send_message(msg)
print(f'Email sent to {recipient}')
except Exception as e:
print(f'Failed to send email to {recipient}: {e}')
def format_email(body, name):
return body.replace('[NAME]', name)
def get_attachments(directory):
return [os.path.join(directory, filename) for filename in os.listdir(directory) if not filename.startswith('.')]
# Main
contacts = read_csv(file_path)
email_templates = {
'CA': read_txt('docs/email_CA.txt'),
'ES': read_txt('docs/email_ES.txt')
}
all_attachments = get_attachments('attachments')
for contact in contacts:
if '@' not in contact[recipient_col]:
print(f'Invalid email address for {contact[name_col]}: {contact[recipient_col]}')
continue
locale_body = email_templates.get(contact[language_col], email_templates['CA'])
send_email(
subject=email_subject,
body=format_email(locale_body, contact[name_col]),
recipient=contact[recipient_col],
attachments=all_attachments
)