-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
36 lines (31 loc) · 1.28 KB
/
main.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
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
MY_EMAIL = '[email protected]'
APP_PASSWORD = 'your_app_password'
class Email:
def __init__(self, from_email, app_password):
self.MY_EMAIL = from_email
self.APP_PASSWORD = app_password
def send_text(self, to_email, text, subject: str = None, type_: str = 'plain'):
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = self.MY_EMAIL
msg['To'] = to_email
msg.attach(MIMEText(text, type_))
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(MY_EMAIL, self.APP_PASSWORD)
smtp.send_message(msg)
def send_file(self, to_email: str, file_path: str, subject: str = None):
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = self.MY_EMAIL
msg['To'] = to_email
filename = file_path
with open(filename) as file:
attachment = MIMEText(file.read())
attachment.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(attachment)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(self.MY_EMAIL, self.APP_PASSWORD)
smtp.send_message(msg)