Compose the Email in Python
Once you’ve securely loaded your credentials, the next step is to create and structure your email using Python’s EmailMessage class.
Step 1: Create a New EmailMessage Object
from email.message import EmailMessage
msg = EmailMessage()
This msg object will store everything about your email: the subject, sender, receiver, body, and even attachments.
Step 2: Set the Email Headers
msg["Subject"] = "Weekly Sales Report"
msg["From"] = sender
msg["To"] = "team@example.com"
-
“Subject”: Sets the subject line of the email
-
“From”: The sender’s email address (your email)
-
“To”: The recipient’s email address
Step 3: Write the Body of the Email
msg.set_content("""
Hello Team,
Please find attached the weekly sales report.
Kind regards,
Your Automation Script
""")
-
set_content()is used to add a plain-text message body. -
Triple quotes (
""") allow you to write multi-line content.
