Python Sending Email
Email is one of the most important communication tools in modern applications.
Python makes it easy to send emails automatically using the SMTP (Simple Mail Transfer Protocol).
You can use Python email automation for:
- Sending notifications
- Password reset emails
- Reports and alerts
- Marketing emails
- Automation systems
What is SMTP?
SMTP (Simple Mail Transfer Protocol) is a protocol used to send emails over the internet.
Python provides built-in support through the smtplib module.
Python Email Modules
Python provides:
- smtplib → sending emails
- email → creating email content
1. Sending a Simple Email
import smtplib
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login("your_email@gmail.com", "your_password")
message = "Hello from Python!"
server.sendmail("your_email@gmail.com", "receiver@gmail.com", message)
server.quit()Important Note
For Gmail:
- Enable 2-Step Verification
- Use App Password (not normal password)
2. Sending Email with Subject
import smtplib
sender = "your_email@gmail.com"
receiver = "receiver@gmail.com"
message = """Subject: Python Email
Hello! This is a test email from Python.
"""
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(sender, "your_password")
server.sendmail(sender, receiver, message)
server.quit()3. Using email.message Module
from email.message import EmailMessage
import smtplib
msg = EmailMessage()
msg["Subject"] = "Python Email"
msg["From"] = "your_email@gmail.com"
msg["To"] = "receiver@gmail.com"
msg.set_content("Hello! This is a Python email.")
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login("your_email@gmail.com", "your_password")
server.send_message(msg)
server.quit()4. Sending HTML Email
msg.set_content("This is plain text")
msg.add_alternative("""
<html>
<body>
<h1>Hello from Python</h1>
<p>This is an HTML email</p>
</body>
</html>
""", subtype="html")5. Sending Email with Attachment
from email.message import EmailMessage
import smtplib
msg = EmailMessage()
msg["Subject"] = "Report"
msg["From"] = "your_email@gmail.com"
msg["To"] = "receiver@gmail.com"
msg.set_content("Please find attached file.")
with open("file.txt", "rb") as f:
file_data = f.read()
file_name = f.name
msg.add_attachment(file_data, maintype="application", subtype="octet-stream", filename=file_name)
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login("your_email@gmail.com", "your_password")
server.send_message(msg)
server.quit()6. Using Gmail SMTP Settings
| Setting | Value |
|---|---|
| SMTP Server | smtp.gmail.com |
| Port | 587 |
| Security | TLS |
7. Common SMTP Errors
Authentication Error
SMTPAuthenticationErrorSolution:
- Use App Password
- Enable Less Secure Apps (not recommended)
Connection Error
Check:
- Internet connection
- SMTP server address
- Port number
8. Sending Multiple Emails
recipients = ["a@gmail.com", "b@gmail.com"]
for email in recipients:
server.sendmail(sender, email, message)9. Email Automation Example
import smtplib
from email.message import EmailMessage
def send_email(to):
msg = EmailMessage()
msg["Subject"] = "Daily Report"
msg["From"] = "your_email@gmail.com"
msg["To"] = to
msg.set_content("This is your daily report.")
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login("your_email@gmail.com", "password")
server.send_message(msg)
server.quit()
send_email("receiver@gmail.com")10. Security Best Practices
- Never hardcode passwords
- Use environment variables
- Use App Passwords for Gmail
- Avoid exposing credentials in code
11. Alternative Email Services
You can also use:
- Outlook SMTP
- Yahoo SMTP
- SendGrid API
- Mailgun API
Real-World Applications
Python email automation is used in:
- E-commerce notifications
- Account verification systems
- Report generation systems
- Alerts and monitoring tools
- Marketing automation
Best Practices
- Use
EmailMessageclass - Always secure credentials
- Use HTML emails for better design
- Handle exceptions properly
- Test with dummy accounts first
Summary
Python provides powerful tools to send emails using SMTP. With simple modules like smtplib and email, developers can automate email communication efficiently.
Conclusion
Email automation is an essential skill for Python developers. It enables you to build real-world applications like notification systems, reporting tools, and automated workflows with ease.


0 Comments