Python Script for Sending Email with Attachment via SMTP
This article demonstrates a Python script that formats email addresses, composes a multipart message, attaches a JPEG screenshot, and sends it through an SMTP server, providing a practical example for UI automation test teams to automatically notify developers of errors.
The article presents a ready‑to‑use Python example for automatically sending an email that includes a screenshot attachment, a common pattern used by UI automation testers to report errors to developers.
The script imports necessary modules from the email package, defines a helper function _format_addr to format addresses, sets sender and receiver credentials, constructs a MIMEMultipart message with proper headers, attaches a text part, reads a JPEG file, encodes it as a base64 attachment, and finally connects to the SMTP server on port 25 to send the email.
Key steps include:
Importing email and smtplib modules.
Formatting sender and recipient addresses with proper UTF‑8 encoding.
Creating a multipart message, adding subject, and attaching plain text.
Reading the image file, setting appropriate MIME headers, and encoding it.
Establishing an SMTP connection, enabling debug output, logging in, sending the message, and closing the connection.
At the end, the article encourages readers to follow the associated public account for more automation learning resources.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
