-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_email.py
96 lines (77 loc) · 2.48 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
if not os.path.exists(".replit"):
from dotenv import load_dotenv
load_dotenv()
def get_email_body() -> str:
"""
Reads the contents of an html file and returns it as the email body text.
"""
html_path = os.path.join("ProsePal", "email_content.html")
with open (html_path, "r") as f:
email_body = f.read()
return email_body
def send_mail(folder_name: str, book_name: str, user_email: str) -> None:
"""
Send user the pdf of their story bible.
Arguments:
folder_name: Name of the folder containing the story bible.
book_name: Name of the book.
user_email: Email address of the user.
"""
password = os.environ['mail_password']
username = os.environ['mail_username']
server = "prosepal.io"
port = 465
file_path = os.path.join(folder_name, f"{book_name}-lorebinder.pdf")
email_body = get_email_body()
try:
s = smtplib.SMTP_SSL(host = server, port = port)
s.login(username, password)
msg = MIMEMultipart()
msg["To"] = user_email
msg["From"] = username
msg["Subject"] = "Your PlotBinder is ready"
with open(file_path, "rb") as attachment:
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f"attachment; filename= {file_path}")
msg.attach(part)
msg.attach(MIMEText(email_body, "html"))
s.send_message(msg)
print("email sent")
except Exception as e:
print(f"Failed to send email. Reason: {e}")
return
def email_error(error: str) -> None:
"""
Send user the pdf of their story bible.
Arguments:
folder_name: Name of the folder containing the story bible.
book_name: Name of the book.
user_email: Email address of the user.
"""
error_email = os.environ['error_email']
password = os.environ['mail_password']
username = os.environ['mail_username']
server = "prosepal.io"
port = 465
email_body = error
try:
s = smtplib.SMTP_SSL(host = server, port = port)
s.login(username, password)
msg = MIMEMultipart()
msg["To"] = error_email
msg["From"] = username
msg["Subject"] = "A critical error occured"
msg.attach(MIMEText(email_body, "html"))
s.send_message(msg)
print("email sent")
except Exception as e:
print(f"Failed to send email. Reason: {e}")
return