SoFunction
Updated on 2025-04-14

Python uses scripts to automatically send emails

To write a Python script to automatically send emails, you can use the smtplib library to handle the SMTP protocol, and the email library to build the email content.

1. Install the necessary libraries

Typically, the smtplib and email libraries are part of the Python standard library, so no additional installation is required. If you are using an older Python version, you may want to make sure that these libraries are installed.

2. Write scripts

Here is a complete Python script example for sending emails with attachments

import smtplib
from  import MIMEMultipart
from  import MIMEText
from  import MIMEBase
from email import encoders

def send_email(sender_email, sender_password, receiver_email, subject, body, attachment_path):
    # Set up the SMTP server    smtp_server = ''  # Replace with your SMTP server address    smtp_port = 587  # Replace with your SMTP server port
    # Create a mail object    msg = MIMEMultipart()
    msg['From'] = sender_email
    msg['To'] = receiver_email
    msg['Subject'] = subject

    # Add email text    (MIMEText(body, 'plain'))

    # Add attachment    if attachment_path:
        attachment = open(attachment_path, 'rb')
        part = MIMEBase('application', 'octet-stream')
        part.set_payload(())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', f'attachment; filename={attachment_path}')
        (part)
        ()

    # Connect to the SMTP server and send emails    try:
        server = (smtp_server, smtp_port)
        ()  # Enable TLS encryption        (sender_email, sender_password)
        text = msg.as_string()
        (sender_email, receiver_email, text)
        ()
        print("Email sent successfully")
    except Exception as e:
        print(f"Email sending failed: {e}")

if __name__ == "__main__":
    # Replace with your sender's email and password    sender_email = 'your_email@'
    sender_password = 'your_password'

    # Replace with the recipient's email    receiver_email = 'receiver_email@'

    # Email Subject and Text    subject = 'Test email'
    body = 'This is a test email with attachments.  '

    # Attachment path (optional)    attachment_path = ''  # Replace with your attachment file path
    # Send email    send_email(sender_email, sender_password, receiver_email, subject, body, attachment_path)

3. Run the script

Save the above script as a Python file (such as send_email.py) and run it on the command line:

python send_email.py

4. Things to note

SMTP server: You need to replace smtp_server and smtp_port for your email service provider's SMTP server address and port. For example, Gmail's SMTP server is, and the port is 587.

Sender Email and Password: You need to replace sender_email and sender_password for your sender email and password. For Gmail, you may need to generate an app-specific password.

Recipient email: Replace receiver_email as the recipient's email address.

Attachment: If you do not need to send attachments, you can set attachment_path to None.

5. Security

Password security: Don't hardcode passwords in scripts, especially if you share or upload code to a public repository. Consider using environment variables or configuration files to manage sensitive information.

TLS encryption: Make sure to enable TLS encryption using starttls() to protect the security of email content during transmission.

This is the article about Python using scripts to automatically send emails. For more information about Python's automatic sending of emails, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!