Python Program to Send Email Using smtplib
smtplib to send an email by creating an SMTP connection, logging in, and calling sendmail() with sender, receiver, and message strings.Examples
How to Think About It
smtplib.SMTP. Then log in with your email and password. Next, create the email content as a string with headers like Subject. Finally, use sendmail() to send the email from sender to receiver.Algorithm
Code
import smtplib sender = 'your_email@example.com' receiver = 'receiver_email@example.com' password = 'your_password' subject = 'Test Email' body = 'Hello, this is a test email sent from Python!' message = f"Subject: {subject}\n\n{body}" with smtplib.SMTP('smtp.gmail.com', 587) as server: server.starttls() server.login(sender, password) server.sendmail(sender, receiver, message) print('Email sent successfully')
Dry Run
Let's trace sending an email with sender='your_email@example.com', receiver='receiver_email@example.com', subject='Test Email', and body='Hello, this is a test email sent from Python!'
Create message string
message = 'Subject: Test Email\n\nHello, this is a test email sent from Python!'
Connect to SMTP server
Connect to 'smtp.gmail.com' on port 587
Start TLS encryption
Secure the connection with starttls()
Login
Login with sender='your_email@example.com' and password='your_password'
Send email
Send email from sender to receiver with the message
Close connection
SMTP connection is closed automatically by with-statement
| Step | Action | Value |
|---|---|---|
| 1 | Create message | Subject: Test Email Hello, this is a test email sent from Python! |
| 2 | Connect SMTP | smtp.gmail.com:587 |
| 3 | Start TLS | Connection secured |
| 4 | Login | your_email@example.com / your_password |
| 5 | Send email | From your_email@example.com to receiver_email@example.com |
| 6 | Close connection | Connection closed |
Why This Works
Step 1: Create the email message
The message string includes a Subject header followed by two newlines and the email body, which is the format SMTP expects.
Step 2: Connect and secure SMTP
Connecting to the SMTP server and calling starttls() encrypts the connection to keep your login and email safe.
Step 3: Login and send
You log in with your email and password, then use sendmail() to send the email from sender to receiver.
Alternative Approaches
import smtplib from email.message import EmailMessage msg = EmailMessage() msg['Subject'] = 'Hello' msg['From'] = 'your_email@example.com' msg['To'] = 'receiver_email@example.com' msg.set_content('This is a test email using EmailMessage class.') with smtplib.SMTP('smtp.gmail.com', 587) as server: server.starttls() server.login('your_email@example.com', 'your_password') server.send_message(msg) print('Email sent successfully')
import yagmail yag = yagmail.SMTP('your_email@example.com', 'your_password') yag.send('receiver_email@example.com', 'Subject here', 'Body text here') print('Email sent successfully')
Complexity: O(1) time, O(1) space
Time Complexity
Sending an email involves a fixed number of network operations, so time complexity is constant O(1).
Space Complexity
Memory usage is constant O(1) as only a small message string and connection objects are stored.
Which Approach is Fastest?
Using smtplib directly is fast and standard; libraries like yagmail add convenience but may add slight overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| smtplib basic | O(1) | O(1) | Simple emails, no extra packages |
| email.message.EmailMessage | O(1) | O(1) | Complex emails with attachments |
| yagmail library | O(1) | O(1) | Simpler syntax, external dependency |
starttls() to secure your SMTP connection before logging in.starttls() causes login failures due to insecure connection.