-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnewsletter.py
53 lines (44 loc) · 1.83 KB
/
newsletter.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
import smtplib
from email.mime.base import MIMEBase
from email import encoders
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import os
def send_mail(to_mail, positive, negative):
"""
Sends an email of investment rentability.
ALl other info is foudn and created in in email_generator.py
Args:
to_mail: email of reciever
positive: stock symb that had best performance
negative: stock symb with worst performance
"""
# Email Info
email_address = os.environ.get('bolsaMAIL')
email_password = os.environ.get('bolsaPASS')
msg = MIMEMultipart()
msg['Subject'] = 'Suas Ações!'
msg['From'] = email_address
msg['to'] = to_mail
plain_msg = MIMEText('Aqui está o resumo de seus investimentos! '
'\nHabilite html para garantir boa visualização deste e-mail', 'plain', 'utf-8')
msg.attach(plain_msg)
# html of the newsletter
html = open('newsletter.html', encoding='utf-8')
msg.attach(MIMEText(html.read(), 'html', 'utf-8'))
# Attachments to show inline, numbered in imglist order. Shows as 'cid:i' on html file.
imglist = ['rentability.png', 'portfolio.png', 'bars.png', f'{positive}.png', f'{negative}.png']
i = 0
for img in imglist:
with open(f'Figures/{img}', 'rb') as f:
mime = MIMEBase('image', 'png', filename=f'{img}')
mime.add_header('Content-Disposition', 'attachment', filename=f'{img}')
mime.add_header('X-Attachment-Id', f'{i}')
mime.add_header('Content-ID', f'<{i}>')
mime.set_payload(f.read())
encoders.encode_base64(mime)
msg.attach(mime)
i += 1
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(email_address, email_password)
smtp.send_message(msg)