The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a contact form parser

A contact form parser is a script or application that processes form submissions—extracting relevant information like name, email, subject, and message from the form data (usually from POST requests), validates them, and optionally stores or sends them via email or to a database.

Here’s a simple but functional contact form parser in Python using Flask:


📌 Python (Flask) Contact Form Parser

python
from flask import Flask, request, jsonify import re app = Flask(__name__) # Email validation regex EMAIL_REGEX = re.compile(r"[^@]+@[^@]+.[^@]+") # Route to handle contact form submissions @app.route('/contact', methods=['POST']) def contact_form_parser(): data = request.form name = data.get('name', '').strip() email = data.get('email', '').strip() subject = data.get('subject', '').strip() message = data.get('message', '').strip() # Basic validation errors = {} if not name: errors['name'] = 'Name is required.' if not email or not EMAIL_REGEX.match(email): errors['email'] = 'Valid email is required.' if not subject: errors['subject'] = 'Subject is required.' if not message: errors['message'] = 'Message cannot be empty.' if errors: return jsonify({'status': 'error', 'errors': errors}), 400 # For demonstration, just echo the parsed and validated data return jsonify({ 'status': 'success', 'data': { 'name': name, 'email': email, 'subject': subject, 'message': message } }), 200 if __name__ == '__main__': app.run(debug=True)

💡 How It Works

  1. Receives POST data from a contact form.

  2. Extracts fields: name, email, subject, message.

  3. Validates data:

    • Ensures fields are not empty.

    • Validates email format using regex.

  4. Returns JSON response with either errors or the parsed data.


🔧 Sample HTML Form for Testing

You can test it using this HTML:

html
<form method="POST" action="http://localhost:5000/contact"> <input type="text" name="name" placeholder="Your Name"><br> <input type="email" name="email" placeholder="Your Email"><br> <input type="text" name="subject" placeholder="Subject"><br> <textarea name="message" placeholder="Message"></textarea><br> <button type="submit">Send</button> </form>

📤 Optional: Send Parsed Data via Email

To email the parsed data, you can use Python’s smtplib or a library like Flask-Mail or external APIs like SendGrid or Mailgun.

Let me know if you want to add email sending, file uploads, or database integration.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About