import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Step 1: Define SMTP server details
SMTP_SERVER = "smtp.gmail.com" # Gmail's SMTP server
SMTP_PORT = 587 # TLS port
EMAIL_ADDRESS = "[email protected]" # Your email address
EMAIL_PASSWORD = "your-email-password" # Your email password (use app password for Gmail)
# Step 2: Create the email message
def create_email(sender, recipient, subject, body):
# Create a multipart email (can include both text and HTML)
message = MIMEMultipart()
message["From"] = sender
message["To"] = recipient
message["Subject"] = subject
# Add the email body (plain text)
message.attach(MIMEText(body, "plain"))
return message
# Step 3: Send the email
def send_email():
try:
# Establish a connection to the SMTP server
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls() # Start TLS encryption
server.login(EMAIL_ADDRESS, EMAIL_PASSWORD) # Log in to the server
# Create email content
recipient = "[email protected]"
subject = "Test Email via SMTP"
body = "Hello, this is a test email sent through Python and SMTP!"
email_message = create_email(EMAIL_ADDRESS, recipient, subject, body)
# Send the email
server.sendmail(EMAIL_ADDRESS, recipient, email_message.as_string())
print("Email sent successfully!")
except Exception as e:
print(f"Failed to send email: {e}")
if __name__ == "__main__":
send_email()
smtplib: Provides the functionality to connect to an SMTP server and send emails.
email.mime: Used to construct email messages with different MIME types (e.g., text, HTML).
SMTP_SERVER: The domain of the SMTP server (e.g., smtp.gmail.com for Gmail).
SMTP_PORT: The port used for connecting securely (587 for TLS).
MIMEMultipart: Allows you to add multiple parts to an email (e.g., plain text, HTML, attachments).
MIMEText: Specifies the body of the email (in this case, plain text).
smtplib.SMTP(): Opens a connection to the SMTP server.
server.starttls(): Starts TLS encryption for secure communication.
server.login(): Authenticates with the SMTP server using your email and password.
server.sendmail(): Sends the email from the sender to the recipient.
Uses a try-except block to catch and display any errors during the email sending process.
Use environment variables or a secrets manager to store your email password securely. For Gmail, generate an App Password to use with third-party apps.
You can attach an HTML version of the email body using MIMEText(body, "html").
Use tools like MailHog to test email functionality locally without actually sending emails.
For more advanced use cases, consider libraries like yagmail or smtplib with additional features like file attachments.
Implement retry logic for temporary failures (e.g., network issues).
smtplib Library
email.mime Library
Gmail SMTP Configuration
SendGrid SMTP API
Flask-Mail: A Flask extension for sending emails.
Postfix SMTP Server: Open-source SMTP server.
Build a system that sends email notifications for events (e.g., reminders, alerts).
Create a tool to send bulk emails with personalization (e.g., newsletter).
Develop a contact form for a website that sends form submissions via email.
Build a tool to schedule and send emails at specific times.
Develop a system that monitors email delivery and logs errors.