forked from suryanshsk/Python-Voice-Assistant-Suryanshsk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_email.py
53 lines (44 loc) · 1.91 KB
/
test_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
import smtplib
import os
import re
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Email validation function
def is_valid_email(email):
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
return re.match(pattern, email)
# Function to send an email
def send_email(subject, body, to_email):
try:
# Environment variables for sensitive data
smtp_server = os.getenv('SMTP_SERVER', 'sandbox.smtp.mailtrap.io')
smtp_port = int(os.getenv('SMTP_PORT', 2525))
sender_email = os.getenv('SENDER_EMAIL', 'yourusername')
send_password = os.getenv('SEND_PASSWORD', 'yourpassword')
# Validate recipient email
if not is_valid_email(to_email):
raise ValueError("Invalid recipient email address.")
# Create a MIME multipart message
message = MIMEMultipart()
message['From'] = sender_email
message['To'] = to_email
message['Subject'] = subject
# Attach the email body as plain text
message.attach(MIMEText(body, 'plain'))
# Establish SMTP connection and send email
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls() # Secure the connection
server.login(sender_email, send_password)
server.sendmail(sender_email, to_email, message.as_string())
print(f"Email successfully sent to {to_email}")
except smtplib.SMTPAuthenticationError:
print("Authentication failed. Please check your email username and password.")
except smtplib.SMTPConnectError:
print("Failed to connect to the SMTP server.")
except ValueError as ve:
print(f"Value Error: {ve}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
# Test email sending
send_email("Enhanced Test Email", "This is an enhanced test email body.", "[email protected]")