Glider
Loading...
Searching...
No Matches
SendEmail.py
Go to the documentation of this file.
1import os
2import smtplib
3import sys, traceback
4from email.message import EmailMessage
5from dotenv import load_dotenv
6from BodyEmail import BodyEmail
7load_dotenv()
8
9t = BodyEmail
10def send_report(receiver_email_address, link, month):
11 """Build the header and body email for monthly report
12
13 Args:
14 receiver_email_address (str): client_email
15 link (str): download link
16 month (str): month and year when sales were processed
17 Returns: Nothing
18 """
19 email_subject = f"Your report for {month}"
20 # Set email body text
21 text = t.report_email(month, link)
22 send_email(receiver_email_address, email_subject, text)
23
24def send_unmatched_report(receiver_email_address, month, link, lines, files):
25 """Build the header and body email for unmatched report
26
27 Args:
28 receiver_email_address (str): client_email
29 link (str): download link
30 month (str): month and year when sales were processed
31 lines (int): total lines which don't match with catalogue
32 files (int): files which have lines not matched
33 Returns: Nothing
34 """
35 email_subject = f"Your unmatched report for {month}"
36 # Set email body text
37 text = t.unmatched_email(month, link, lines, files)
38 print(send_email(receiver_email_address, email_subject, text))
39
40def send_statement_report(receiver_email_address, month, csv_link, pdf_link):
41 """Builds the body for statements email. The format used is html
42
43 Args:
44 period (str): month and year when sales were processed
45 csv_link (str): presigned link to download csv statement
46 pdf_link (str): presigned link to download pdf statement
47 Returns: Nothing
48 """
49 email_subject = f"Your Statement report for {month}"
50 # Set email body text
51 text = t.statement_email(month, csv_link, pdf_link)
52 print(send_email(receiver_email_address, email_subject, text))
53
54def send_email(receiver_email_address, email_subject, text):
55 """Sends the download link via email to client
56
57 Args:
58 receiver_email_address (str): client_email
59 email_subject (str): receiver email
60 text (str): body email
61 Returns: response (str)
62 """
63
64 message = EmailMessage()
65
66 sender_email_address = os.environ.get("SENDER_EMAIL")
67 email_password = os.environ.get("SENDER_PASSWORD")
68
69 message.set_content(text, subtype='html')
70
71 # Configure email headers
72 message['Subject'] = email_subject
73 message['From'] = sender_email_address
74 message['To'] = receiver_email_address
75
76
77 # Set smtp server and port
78 email_smtp = "smtp.gmail.com"
79 server = smtplib.SMTP(email_smtp, '587')
80 email_smtp = "smtp.gmail.com"
81
82 # Set smtp server and port
83 server = smtplib.SMTP(email_smtp, '587')
84
85 # Identify this client to the SMTP server
86 server.ehlo()
87
88 # Secure the SMTP connection
89 server.starttls()
90
91 # Login to email account
92 server.login(sender_email_address, email_password)
93
94 # Send email
95 server.send_message(message)
96
97 # Close connection to server
98 server.quit()
99 response = f"Link sent to {receiver_email_address}"
100 return response
send_report(receiver_email_address, link, month)
Definition SendEmail.py:10
send_unmatched_report(receiver_email_address, month, link, lines, files)
Definition SendEmail.py:24
send_statement_report(receiver_email_address, month, csv_link, pdf_link)
Definition SendEmail.py:40
send_email(receiver_email_address, email_subject, text)
Definition SendEmail.py:54