Email python simple
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()