-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailer.py
55 lines (44 loc) · 1.73 KB
/
mailer.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
import smtplib
from datetime import date
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_email(send_from, password, send_to, subject, message):
"""
sends mail without any attachment
"""
for receiver in send_to:
multipart = MIMEMultipart()
multipart["From"] = send_from
multipart["To"] = receiver
multipart["Subject"] = subject
multipart.attach(MIMEText(message, "html"))
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server.login(send_from, password)
server.sendmail(send_from, receiver, multipart.as_string())
server.quit()
def send_email_with_df(dataframes, file_names, send_from, password, send_to, subject):
"""
accepts List of dataframes with list of file names
"""
message = f"""\
<p><strong>Data for the day today </strong></>
<p><br></p>
<p><strong>Regards </strong><br><strong>Aditya MS </strong></p>
"""
for receiver in send_to:
multipart = MIMEMultipart()
multipart["From"] = send_from
multipart["To"] = receiver
multipart["Subject"] = subject
for df, file_name in zip(dataframes, file_names):
attachment = MIMEApplication(df.to_csv())
attachment["Content-Disposition"] = 'attachment; filename="{}"'.format(
f"{file_name}.csv"
)
multipart.attach(attachment)
multipart.attach(MIMEText(message, "html"))
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server.login(send_from, password)
server.sendmail(send_from, receiver, multipart.as_string())
server.quit()