import smtplib
from email.mime.base import MIMEBase
from email import encoders
filename = "example_file.pdf" # Replace with the actual filename
# Open the file in binary mode
with open(filename, "rb") as attachment:
# Create a MIME part with "application/octet-stream" media type
part = MIMEBase("application", "octet-stream")
# Set the payload to the binary data read from the file
part.set_payload(attachment.read())
# Encode the payload as base64
encoders.encode_base64(part)
# Add a header to specify the filename for the attachment
part.add_header("Content-Disposition", f"attachment; filename= {filename}")
# Now you can proceed with sending the email using smtplib
# ...