-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail.py
More file actions
64 lines (56 loc) · 1.88 KB
/
mail.py
File metadata and controls
64 lines (56 loc) · 1.88 KB
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
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
def send_email(subject, body, to_email, attachment_path=None):
# Email configuration
from_email = os.getenv("EMAIL_ADDRESS")
password = os.getenv("EMAIL_PASSWORD")
if not from_email or not password:
raise ValueError("Email credentials are not set in the environment variables.")
# Create a multipart message
msg = MIMEMultipart()
msg['From'] = from_email
msg['To'] = to_email
msg['Subject'] = subject
# Attach the email body
msg.attach(MIMEText(body, 'plain'))
if attachment_path:
try:
with open(attachment_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={os.path.basename(attachment_path)}",
)
msg.attach(part)
except Exception as e:
raise ValueError(f"Failed to attach file: {e}")
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(from_email, password)
server.send_message(msg)
return True
except Exception as e:
raise ConnectionError(f"Failed to send email: {e}")
finally:
server.quit()
# Example usage
if __name__ == "__main__":
try:
send_email(
subject="Test Email",
body="This is a test email.",
to_email="toharivenkat@gmail.com"
)
print("Email sent successfully.")
except Exception as e:
print(f"Error: {e}")