-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
30 lines (25 loc) · 1.06 KB
/
tasks.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
from dotenv import load_dotenv
from Google import Create_Service
import base64
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
load_dotenv()
def send_user_registration_email(subject, message, user_email):
CLIENT_SECRET_FILE = 'client_secret.json'
API_NAME = 'gmail'
API_VERSION = 'v1'
SCOPES = ['https://mail.google.com/']
service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
#emailMsg = f'Welcome to our platform, {user}!'
mimeMessage = MIMEMultipart()
mimeMessage['to'] = user_email
mimeMessage['subject'] = subject
mimeMessage.attach(MIMEText(message, 'plain'))
raw_string = base64.urlsafe_b64encode(mimeMessage.as_bytes()).decode()
try:
message = service.users().messages().send(userId='me', body={'raw': raw_string}).execute()
print("Email sent successfully:", message)
except Exception as e:
print("An error occurred while sending the email:", e)
# Example usage:
#send_user_registration_email('Welcomre!', 'Jorhn Doe', 'shakilmrf8@gmail.com')