10 minute mail with confirmation
A 10-minute mail with confirmation! That's a great idea. Here's a simple implementation using Python and the smtplib
library:
Note: This code is for educational purposes only. You should not use it for sending spam or unsolicited emails.
Code:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import time
# Set up email server
SMTP_SERVER = "smtp.gmail.com"
SMTP_PORT = 587
FROM_EMAIL = "[email protected]"
PASSWORD = "your_password"
# Set up recipient email
TO_EMAIL = "[email protected]"
# Create a message
msg = MIMEMultipart()
msg["Subject"] = "10-Minute Mail Confirmation"
msg["From"] = FROM_EMAIL
msg["To"] = TO_EMAIL
# Add a confirmation link
confirmation_link = f"https://example.com/confirm?email={TO_EMAIL}&code={time.time()}"
msg.attach(MIMEText(f"Please confirm your email by clicking this link: {confirmation_link}", "plain"))
# Send the email
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
server.starttls()
server.login(FROM_EMAIL, PASSWORD)
server.sendmail(FROM_EMAIL, TO_EMAIL, msg.as_string())
server.quit()
print("Email sent!")
How it works:
- The code sets up an email server using
smtplib
. - It creates a message with a subject, from address, and to address.
- It adds a confirmation link to the message body, which includes a unique code generated using the current timestamp.
- The code sends the email using the
sendmail
method. - The recipient receives the email and can click the confirmation link to verify their email address.
Confirmation link:
The confirmation link is generated using the following format:
https://example.com/confirm?email=<recipient_email>&code=<unique_code>
When the recipient clicks the link, your server can verify the email address and confirmation code to confirm the email address.
Example confirmation handler:
import requests
@app.route("/confirm", methods=["GET"])
def confirm_email():
email = request.args.get("email")
code = request.args.get("code")
if code == str(time.time()):
# Verify the email address and confirmation code
#...
return "Email confirmed!"
else:
return "Invalid confirmation code", 400
This is a basic example, and you should add proper error handling and security measures to your confirmation handler.