-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsend_email.py
executable file
·46 lines (39 loc) · 1.33 KB
/
send_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
import os
from email.policy import SMTP
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email import encoders
def send_email_func():
# Path
dir_path = os.path.dirname(os.path.realpath(__file__))
# Creds
creds = []
with open('creds.txt') as f:
for line in f:
creds.append(line)
# Sender, Receiver, subject, body
sender = '[email protected]'
receivers = ['[email protected]']
body = 'Hello Dev,\n\nHere is your daily betting report.'
subject = 'Daily Betting Report'
# Create message
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = ','.join(receivers)
msg['Body'] = body
textPart = MIMEText(body, 'plain')
msg.attach(textPart)
# Add file
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(f'{dir_path}/tmp/report.pdf', 'rb').read())
encoders.encode_base64(part)
part.add_header(f'Content-Disposition', 'attachment; filename="{dir_path} + /tmp/report.pdf"')
msg.attach(part)
# Connect to Gmail SMTP server
s = smtplib.SMTP_SSL(host='smtp.gmail.com', port=465)
s.login(user = creds[0], password = creds[1])
s.sendmail(sender, receivers, msg.as_string())
s.quit()