-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEmail.py
51 lines (43 loc) · 1.52 KB
/
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
47
48
49
50
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from datetime import datetime
import os
from os.path import join, dirname
from dotenv import load_dotenv
from helpers import *
dotenv_path = join(dirname(__file__), '.env')
local_env = load_dotenv(dotenv_path)
if local_env:
GMAIL_EMAIL = os.environ.get("GMAIL_EMAIL")
GMAIL_PASSWORD = os.environ.get("GMAIL_PASSWORD")
GMAIL_RECEIVER_ADDRESS = os.environ.get("GMAIL_RECEIVER_ADDRESS")
else:
GMAIL_EMAIL = os.environ.get("GMAIL_EMAIL")
GMAIL_PASSWORD = os.environ.get("GMAIL_PASSWORD")
GMAIL_RECEIVER_ADDRESS = os.environ.get("GMAIL_RECEIVER_ADDRESS")
class Gmail(object):
def __init__(self, email, password):
self.email = GMAIL_EMAIL
self.password = GMAIL_PASSWORD
self.server = 'smtp.gmail.com'
self.port = 587
session = smtplib.SMTP(self.server, self.port)
session.ehlo()
session.starttls()
session.ehlo
session.login(self.email, self.password)
self.session = session
def send_email(self, subject, body):
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = self.email
msg['To'] = GMAIL_RECEIVER_ADDRESS
msg['MIME-Version'] = '1.0'
msg['Content-Type'] = 'text/html'
body_send = MIMEText(body, 'html')
msg.attach(body_send)
self.session.sendmail(
self.email,
GMAIL_RECEIVER_ADDRESS,
msg.as_string())