Email python simple

From UVOO Tech Wiki
Revision as of 15:32, 8 April 2024 by Busk (talk | contribs) (Created page with "``` import smtplib # SMTP server details SMTP_SERVER = 'smtp.example.com' SMTP_PORT = 25 # Sender and recipient details FROM = 'sender@example.com' TO = 'recipient@example.c...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
import smtplib

# SMTP server details
SMTP_SERVER = 'smtp.example.com'
SMTP_PORT = 25

# Sender and recipient details
FROM = 'sender@example.com'
TO = 'recipient@example.com'

# Email content
subject = 'Test email'
body = 'This is a test email sent using Python SMTP without TLS.'

# Construct the email message
message = f"Subject: {subject}\nFrom: {FROM}\nTo: {TO}\n\n{body}"

try:
    # Connect to the SMTP server
    server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)

    # Send the email
    server.sendmail(FROM, TO, message)
    print("Email sent successfully!")

except Exception as e:
    print("Failed to send email:", e)

finally:
    # Close the connection to the SMTP server
    server.quit()