-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage_email.py
87 lines (66 loc) · 2.89 KB
/
manage_email.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
86
87
"""Use to send reminder emails to clients. Uses client's personal name in each email."""
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
from string import Template
from typing import List
from client import Client
"""*****************BE AWARE YOUR EMAIL PASSWORD WILL BE WRITTEN IN THIS FILE*****************"""
# Modify these constants
sender_name = "Business owner name"
contact_number = "123-456-7890"
sender_email = "**********"
sender_password = "**********"
# For gmail you will need to allow less secure apps to access your account. Also, use the following defaults:
# host_address = 'smtp.gmail.com'
# port_number = 465
host_address = "host_address"
port_number = "port_number"
def validate_email(email: str) -> bool:
"""Returns false if email does not contain '@' sign."""
if not email:
return True
if "@" in email:
return True
return False
def create_template(filename) -> Template:
"""Returns a Template object to be used in crafting emails."""
with open(filename, 'r', encoding='utf-8') as template_file:
template_file_content = template_file.read()
return Template(template_file_content)
def create_message(recipient_list: List[Client], business_number: str, your_name: str) ->List[MIMEMultipart]:
"""
Creates an email message text file. Returns a list of MIMEMultipart messages for each recipient with an email.
Make sure to edit message.txt with your custom message.
"""
msg_list = []
for recipient in recipient_list:
if recipient.get_email():
message_template = create_template('message.txt')
msg = MIMEMultipart()
message = message_template.substitute(
PERSON_NAME=recipient.get_first_name(),
PHONE=business_number, YOUR_NAME=your_name)
msg['From'] = sender_email
msg['To'] = recipient.get_email()
msg['Subject'] = "We miss you!"
msg.attach(MIMEText(message, 'plain'))
msg_list.append(msg)
return msg_list
def make_email_list(recipient_list: List[Client]) -> List[str]:
"""Returns an email list for all clients that have an email."""
return [client.get_email() for client in recipient_list if client.get_email()]
def send_email(recipient_list: List[Client]) -> None:
"""Logs in and sends an email to each recipient in the recipient list."""
try:
email_list = make_email_list(recipient_list)
msg_list = create_message(recipient_list, contact_number, sender_name)
server = smtplib.SMTP_SSL(host_address, port_number)
server.ehlo()
server.login(sender_email, sender_password)
for i in range(len(recipient_list)):
server.send_message(msg_list[i], sender_email, email_list[i])
server.close()
print('Email(s) sent!')
except:
print("Something went wrong!")