-
Notifications
You must be signed in to change notification settings - Fork 0
/
igmail.py
93 lines (56 loc) · 2.33 KB
/
igmail.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
88
89
90
91
92
93
# igmail.py
import smtplib
import random
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
class IGMail:
def __init__(self, smtp_from, smtp_to, smtp_server, smtp_port, smtp_username, smtp_password):
self.smtp_from = smtp_from
self.smtp_to = smtp_to
self.smtp_server= smtp_server
self.smtp_port = smtp_port
self.smtp_username = smtp_username
self.smtp_password = smtp_password
EMOJIS = [
'🤢',
'🤮',
'🥱',
'🤓',
'💀',
'💩',
'🧻',
'🍤',
'👎',
'😂'
]
def build_email(self, lost_followers: set, new_followers: set, one_way_set: set, last_ran: str) -> MIMEMultipart:
'''Build and return MIME compliant message'''
msg = MIMEMultipart('alternative')
msg['Subject'] = 'Instagram Followers Update'
msg['From'] = self.smtp_from
msg['To'] = self.smtp_to
text = 'Users not following you back:\n'
losers = ''
for lost in lost_followers:
losers += f'<li>{random.choice(self.EMOJIS)}{lost}{random.choice(self.EMOJIS)}</li>\n'
text += lost
cool_people = ''
for new in new_followers:
cool_people += f'<li>😎{new}😎</li>\n'
one_way = ''
for one in one_way_set:
one_way += f'<li>{random.choice(self.EMOJIS)}{one}{random.choice(self.EMOJIS)}</li>\n'
with open(r'templates/email.html', 'r') as email:
html = email.read().format(losers=losers, cool_people=cool_people, one_way=one_way, last_ran=last_ran)
msg.attach(MIMEText(text, 'plain'))
msg.attach(MIMEText(html, 'html'))
return msg
def send(self, msg: MIMEMultipart):
'''Send followers update email'''
smtp_connection = smtplib.SMTP(self.smtp_server, self.smtp_port)
smtp_connection.ehlo()
smtp_connection.starttls()
smtp_connection.ehlo()
smtp_connection.login(self.smtp_username, self.smtp_password)
smtp_connection.sendmail(self.smtp_from, self.smtp_to, msg.as_string())
print(f'Sent email to {self.smtp_to}')