0% found this document useful (0 votes)
4 views2 pages

Tests

Uploaded by

SQL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Tests

Uploaded by

SQL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

5000

5001
5010
5011
5100
5101
5110
5111

10000
10002
10020
10022
10200
10202
10220
10222
12000
12002
12020
12022
12200
12202
12220
12222

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

def send_email_with_attachment(subject, body, to_email, from_email, password,


attachment_path):
# Create the email
msg = MIMEMultipart()
msg['From'] = from_email
msg['To'] = to_email
msg['Subject'] = subject

# Attach the body of the email


msg.attach(MIMEText(body, 'plain'))

# Attach the file


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= {attachment_path}',
)
msg.attach(part)
# Set up the server and send the email
try:
with smtplib.SMTP('smtp.gmail.com', 587) as server:
server.starttls() # Secure the connection
server.login(from_email, password) # Log in to your email account
server.sendmail(from_email, to_email, msg.as_string()) # Send the
email
print("Email sent successfully")
except Exception as e:
print(f"Error: {e}")

# Usage example
subject = "Subject of the email"
body = "Body of the email"
to_email = "[email protected]"
from_email = "[email protected]"
password = "your-email-password"
attachment_path = "path/to/your/attachment.txt"

send_email_with_attachment(subject, body, to_email, from_email, password,


attachment_path)

You might also like