Temporary email addresses are useful for many reasons, such as avoiding spam or signing up for websites without using your primary email address. In this guide, we’ll create a temporary email system using Python. This includes generating a random email address, sending emails, and checking the inbox.

1. Generate a Random Email Address

To create a temporary email, we’ll generate a random email address using the random module.

import random
import string

def generate_temp_email(domain='example.com'):
    username = ''.join(random.choices(string.ascii_lowercase + string.digits, k=10))
    return f"{username}@{domain}"

temp_email = generate_temp_email()
print(f"Temporary Email: {temp_email}")

This code generates a random username of 10 characters and appends the domain example.com.

2. Set Up SMTP for Sending Emails

To send emails to the temporary address, we need to configure an SMTP server. In this example, we’ll use Gmail’s SMTP server.

import smtplib
from email.mime.text import MIMEText

def send_email(to_address, subject, body):
    from_address = '[email protected]'
    password = 'your-email-password'
    
    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['From'] = from_address
    msg['To'] = to_address

    with smtplib.SMTP('smtp.gmail.com', 587) as server:
        server.starttls()
        server.login(from_address, password)
        server.sendmail(from_address, to_address, msg.as_string())

# Example usage
send_email(temp_email, "Test Subject", "This is a test email.")

This code sends an email with the specified subject and body to the temporary email address using Gmail’s SMTP server.

3. Create a Simple Server to Receive Emails

To complete the temporary email system, we need a server to receive emails. We’ll use the Flask framework and imaplib module for this.

from flask import Flask
import imaplib
import email

app = Flask(__name__)

# IMAP server configuration for receiving emails
IMAP_SERVER = 'imap.gmail.com'
IMAP_PORT = 993
EMAIL_ACCOUNT = '[email protected]'
PASSWORD = 'your-email-password'

def check_inbox():
    mail = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT)
    mail.login(EMAIL_ACCOUNT, PASSWORD)
    mail.select('inbox')

    status, messages = mail.search(None, 'ALL')
    email_ids = messages[0].split()
    
    for email_id in email_ids:
        status, msg_data = mail.fetch(email_id, '(RFC822)')
        msg = email.message_from_bytes(msg_data[0][1])
        print(f"From: {msg['from']}\nSubject: {msg['subject']}\n\n{msg.get_payload(decode=True).decode('utf-8')}")

    mail.logout()

@app.route('/check_mail', methods=['GET'])
def check_mail():
    check_inbox()
    return "Checked Inbox"

if __name__ == '__main__':
    app.run(port=5000)

This script sets up a simple web server with Flask that checks the email inbox and prints the contents of all emails.

4. Putting It All Together

Now, we’ll combine the steps to generate a temporary email address, send an email, and check the inbox in one script.

import random
import string
import smtplib
from email.mime.text import MIMEText
import imaplib
import email
from flask import Flask

app = Flask(__name__)

def generate_temp_email(domain='example.com'):
    username = ''.join(random.choices(string.ascii_lowercase + string.digits, k=10))
    return f"{username}@{domain}"

def send_email(to_address, subject, body):
    from_address = '[email protected]'
    password = 'your-email-password'
    
    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['From'] = from_address
    msg['To'] = to_address

    with smtplib.SMTP('smtp.gmail.com', 587) as server:
        server.starttls()
        server.login(from_address, password)
        server.sendmail(from_address, to_address, msg.as_string())

IMAP_SERVER = 'imap.gmail.com'
IMAP_PORT = 993
EMAIL_ACCOUNT = '[email protected]'
PASSWORD = 'your-email-password'

def check_inbox():
    mail = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT)
    mail.login(EMAIL_ACCOUNT, PASSWORD)
    mail.select('inbox')

    status, messages = mail.search(None, 'ALL')
    email_ids = messages[0].split()
    
    for email_id in email_ids:
        status, msg_data = mail.fetch(email_id, '(RFC822)')
        msg = email.message_from_bytes(msg_data[0][1])
        print(f"From: {msg['from']}\nSubject: {msg['subject']}\n\n{msg.get_payload(decode=True).decode('utf-8')}")

    mail.logout()

@app.route('/check_mail', methods=['GET'])
def check_mail():
    check_inbox()
    return "Checked Inbox"

if __name__ == '__main__':
    temp_email = generate_temp_email()
    print(f"Temporary Email: {temp_email}")
    
    send_email(temp_email, "Test Subject", "This is a test email.")
    app.run(port=5000)

Conclusion

This guide demonstrates how to create a temporary email system using Python. The script generates a random email address, sends a test email, and checks the inbox. You can further customize and enhance this script based on your specific requirements.

Dependencies and Setup

To run this script, ensure you have the necessary Python packages installed:

pip install Flask

Also, update the script with your email credentials and ensure your email account allows access to less secure apps or uses an app-specific password.

With this setup, you can efficiently manage temporary email addresses for various purposes.

Comments (0)

There are no comments here yet, you can be the first!

Leave a Reply

Your email address will not be published. Required fields are marked *


Choose and Buy Proxy

Datacenter Proxies

Rotating Proxies

UDP Proxies

Trusted By 10000+ Customers Worldwide

Proxy Customer
Proxy Customer
Proxy Customer flowch.ai
Proxy Customer
Proxy Customer
Proxy Customer