Skip to content

Sending Email With SMTP Server This is a Python example of using the built-in smtplib library to send an email via an SMTP server.

License

Notifications You must be signed in to change notification settings

QMS85/Email-With-SMTP-Server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

Sending Email With SMTP Server

This is a Python example of using the built-in smtplib library to send an email via an SMTP server.

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()

Explanation of the Code

1. Importing Libraries:

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).

2. Defining SMTP Server Details:

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).

3. Creating the Email:

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).

4. Establishing a Connection:

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.

5. Sending the Email:

server.sendmail(): Sends the email from the sender to the recipient.

6. Error Handling:

Uses a try-except block to catch and display any errors during the email sending process.

Tips & Tricks

1. Avoid Hardcoding Passwords:

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.

2. Use HTML Emails:

You can attach an HTML version of the email body using MIMEText(body, "html").

3. Test Locally:

Use tools like MailHog to test email functionality locally without actually sending emails.

4. SMTP Libraries:

For more advanced use cases, consider libraries like yagmail or smtplib with additional features like file attachments.

5. Handle Errors Gracefully:

Implement retry logic for temporary failures (e.g., network issues).

Further Resources:

1. Python Documentation:

smtplib Library
email.mime Library

2. SMTP Services:

Gmail SMTP Configuration
SendGrid SMTP API

3. GitHub Projects:

Flask-Mail: A Flask extension for sending emails.
Postfix SMTP Server: Open-source SMTP server.

Project Ideas:

1. Email Notification System:

Build a system that sends email notifications for events (e.g., reminders, alerts).

2. Bulk Email Sender:

Create a tool to send bulk emails with personalization (e.g., newsletter).

3. Contact Form with Email:

Develop a contact form for a website that sends form submissions via email.

4. Email Scheduler:

Build a tool to schedule and send emails at specific times.

5. Email Error Monitoring:

Develop a system that monitors email delivery and logs errors.

About

Sending Email With SMTP Server This is a Python example of using the built-in smtplib library to send an email via an SMTP server.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages