Send Excel and PDF automation reports by email with Python. Build MIME attachments with the standard email package and send through an approved SMTP service.
Architecture: Email Excel + PDF Reports
Email Excel + PDF Reports
SQL / Report
Create Excel or PDF
report.xlsxEmail Message
Set subject/from/to
EmailMessageAttachment
Read file bytes
add_attachmentSMTP
Connect securely
SMTP_SSL / STARTTLSAuthenticate
Use approved account method
login / OAuthSend
Deliver report
send_message1. Use Python Standard Library email tools
The email, smtplib and ssl modules are included with Python. Your organization may require an app password, OAuth or an internal SMTP relay.
2. Build an email message
from email.message import EmailMessage msg = EmailMessage()
msg["Subject"] = "Daily SQF Production Report"
msg["From"] = "reporting@example.com"
msg["To"] = "production@example.com"
msg.set_content("Please find the attached production report.")3. Attach an Excel file
from pathlib import Path path = Path("SQF_Report.xlsx")
data = path.read_bytes()
msg.add_attachment( data, maintype="application", subtype="vnd.openxmlformats-officedocument.spreadsheetml.sheet", filename=path.name,
)4. Attach a PDF file
pdf = Path("SQF_Report.pdf")
msg.add_attachment( pdf.read_bytes(), maintype="application", subtype="pdf", filename=pdf.name,
)5. Send through SMTP securely
import smtplib
import ssl context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.example.com", 465, context=context) as smtp: smtp.login("reporting@example.com", "APP_PASSWORD_OR_SECRET") smtp.send_message(msg)6. Log the delivery result
import logging
logging.info("Report email sent successfully")
Frequently Asked Questions
Does Python include email support?
Yes. The Standard Library includes email message construction and SMTP client modules.
Should I hard-code an email password?
No. Use your organization’s approved secret or authentication method, such as environment variables, credential stores, app passwords or OAuth where required.
Can one email contain both Excel and PDF?
Yes. Add both files as separate MIME attachments before sending.
