Skip to content

rnd-sh/disposable-mail

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

disposable-mail

Comprehensive Guide to Setting Up a Disposable Email Service

A disposable email service allows users to create temporary, anonymous email addresses that can be used for a short period of time and discarded once no longer needed. This is particularly useful for preventing spam or when an email address is needed for a one-time registration or verification.

In this guide, we will walk through the steps to set up a disposable email service, including setting up a backend server, creating temporary email addresses, and configuring email forwarding.

Table of Contents

  1. Prerequisites
  2. Architecture Overview
  3. Setting Up the Backend Server
  4. Creating Temporary Email Addresses
  5. Handling Incoming Emails
  6. Forwarding Emails
  7. Managing Expiration
  8. Frontend Interface
  9. Security Considerations
  10. Deploying the Service
  11. Conclusion

1. Prerequisites

Before setting up the disposable email service, ensure you have the following:

  • A domain: For handling incoming and outgoing emails (e.g., tempemail.com).
  • A server: A Linux server (Ubuntu/Debian recommended) to host the service.
  • A mail server setup: Configure an SMTP and IMAP server (e.g., Postfix for SMTP, Dovecot for IMAP).
  • Docker (optional): For easier deployment and isolation of components.
  • A web framework: We will use Flask in this guide for the web API.
  • A database: To store temporary email addresses and metadata (e.g., SQLite, PostgreSQL).

2. Architecture Overview

The architecture of a disposable email service consists of several components:

  • Web API: To allow users to create temporary email addresses and interact with the service.
  • Mail Server: To handle the sending and receiving of emails.
  • Database: To store metadata related to temporary email addresses (e.g., expiration times).
  • Frontend Interface: An optional UI for users to interact with the service.

3. Setting Up the Backend Server

We'll use Postfix and Dovecot for handling email sending and receiving.

3.1 Install Postfix (SMTP Server)

sudo apt update
sudo apt install postfix

During installation, select "Internet Site" and set the system mail name (e.g., tempemail.com).

Configure Postfix to accept emails for your domain. Edit /etc/postfix/main.cf:

myhostname = tempemail.com
mydomain = tempemail.com
myorigin = /etc/mailname
mydestination = $myhostname, localhost.$mydomain, localhost
relayhost = 
mynetworks = 127.0.0.0/8

Restart Postfix to apply the changes:

sudo systemctl restart postfix

3.2 Install Dovecot (IMAP Server)

sudo apt install dovecot-core dovecot-imapd

Edit the /etc/dovecot/dovecot.conf file to configure Dovecot:

mail_location = maildir:~/Maildir
service imap-login {
   inet_listener imap {
     port = 0
   }
   inet_listener imaps {
     port = 993
     ssl = yes
   }
}

Restart Dovecot:

sudo systemctl restart dovecot

4. Creating Temporary Email Addresses

Create a database to store temporary email addresses and their metadata (e.g., expiration time).

4.1 Database Schema

For simplicity, we'll use SQLite. Here’s an example schema:

CREATE TABLE temp_emails (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    email_address TEXT UNIQUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    expires_at TIMESTAMP
);

4.2 Generate Temporary Email Address

You can generate temporary email addresses by appending random strings to your domain name. For example, if your domain is tempemail.com, you might generate [email protected].

Here’s an example Python function to generate a random email:

import random
import string

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

temp_email = generate_temp_email("tempemail.com")
print(temp_email)  # Example: [email protected]

5. Handling Incoming Emails

You will need to configure your mail server to capture incoming emails and associate them with temporary email addresses.

5.1 Set Up Postfix to Accept Emails for Temporary Addresses

You need to configure Postfix to forward all emails sent to a temporary address to a handler on your server. Add the following to your Postfix configuration (/etc/postfix/main.cf):

virtual_alias_maps = hash:/etc/postfix/virtual

Then, create the /etc/postfix/virtual file and add a line for each temporary email address:

After editing the virtual file, run:

postmap /etc/postfix/virtual
sudo systemctl restart postfix

6. Forwarding Emails

To forward emails to the user's actual inbox, you will need to configure your handler script to forward the email.

6.1 Use a Python Script to Forward Emails

You can use Python with the smtplib and email libraries to forward emails. Here's an example script to forward the email:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

def forward_email(sender, recipient, subject, body):
    msg = MIMEMultipart()
    msg['From'] = sender
    msg['To'] = recipient
    msg['Subject'] = subject
    msg.attach(MIMEText(body, 'plain'))
    
    server = smtplib.SMTP('smtp.yourserver.com', 587)
    server.starttls()
    server.login('your_email', 'your_password')
    server.sendmail(sender, recipient, msg.as_string())
    server.quit()

forward_email('[email protected]', '[email protected]', 'Test Subject', 'Test Body')

7. Managing Expiration

You can automatically delete temporary email addresses after a set period of time. You can store the expires_at timestamp in your database and create a cron job to clean up expired email addresses.

7.1 Cron Job to Delete Expired Emails

Add a cron job that runs daily to remove expired emails:

0 0 * * * /usr/bin/python3 /path/to/delete_expired_emails.py

7.2 Python Script to Delete Expired Emails

In your Python script, check for expired emails and delete them from the database:

import sqlite3
from datetime import datetime

def delete_expired_emails():
    conn = sqlite3.connect('emails.db')
    cursor = conn.cursor()
    cursor.execute("DELETE FROM temp_emails WHERE expires_at < ?", (datetime.now(),))
    conn.commit()
    conn.close()

delete_expired_emails()

8. Frontend Interface

You can create a simple frontend for users to interact with your disposable email service. You can use Flask to create an API and web interface.

8.1 Basic Flask Setup

Install Flask:

pip install Flask

Create a simple Flask app.py:

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)

Create a templates/index.html file for the frontend:

<!DOCTYPE html>
<html>
<head>
    <title>Disposable Email Service</title>
</head>
<body>
    <h1>Create a Temporary Email</h1>
    <form action="/generate_email" method="POST">
        <button type="submit">Generate Email</button>
    </form>
</body>
</html>

9. Security Considerations

  • Prevent abuse: Implement rate limiting and CAPTCHA on the email generation form to prevent abuse.
  • Encrypt communication: Ensure all communication with your service is encrypted using HTTPS (via SSL/TLS).
  • Monitor usage: Regularly monitor the usage of your service to detect and prevent spam.

10. Deploying the Service

Deploy your disposable email service on a server, using tools like Docker, or directly on an Ubuntu/Debian server.

10.1 Docker Deployment

You can create a Docker container for your service by writing a Dockerfile:

FROM python:3.8-slim
RUN pip install Flask smtplib
COPY . /app
WORKDIR /app
CMD ["python", "app.py"]

Build and run the Docker container:

docker build -t disposable-email .
docker run -p 5000:5000 disposable-email

11. Conclusion

Setting up a disposable email service involves configuring a mail server (Postfix and Dovecot), creating temporary email addresses, handling incoming emails, and forwarding them to real email addresses. You can add expiration management and a web frontend for user interaction.

Security considerations such as HTTPS encryption, abuse prevention, and monitoring usage are essential for ensuring the service remains safe and useful.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published