Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
48 lines (43 loc) · 1.62 KB
/
Copy pathmain.py
File metadata and controls
48 lines (43 loc) · 1.62 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
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_email(smtp_server, port, sender_email, password, recipient_email, subject, body):
"""
Sends an email using SMTP.
Args:
smtp_server (str): SMTP server address.
port (int): Port number for the SMTP server.
sender_email (str): Sender's email address.
password (str): Sender's email password.
recipient_email (str): Recipient's email address.
subject (str): Subject of the email.
body (str): Body of the email.
"""
# Create a multipart email message
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = recipient_email
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
try:
# Establish a connection to the SMTP server and start TLS encryption
with smtplib.SMTP(smtp_server, port) as server:
server.starttls()
print("Connecting to the SMTP server...")
server.login(sender_email, password)
print("Logged in successfully!")
server.send_message(msg)
print("Email sent successfully!")
except Exception as e:
print(f"Failed to send email: {e}")
if __name__ == "__main__":
# Configuration
smtp_server = "smtp.gmail.com"
port = 587
sender_email = "yourgmail@gmail.com"
password = "yourpassword"
recipient_email = "recipientemail"
subject = "Test Email"
body = "This is a test email sent from Python."
# Send the email
send_email(smtp_server, port, sender_email, password, recipient_email, subject, body)