Using Python to Transmit HTML Form Data via Mailgun API

Hey folks! I’m trying to figure out how to send data from an HTML form using Python and the Mailgun API. I’ve got the basics down for sending messages, but I’m stuck on how to include form data.

Here’s what I’m working with:

<form action=\"/submit\" method=\"post\">
    <input name=\"user_name\" placeholder=\"Your Name\">
    <!-- other form fields -->
    <button type=\"submit\">Send</button>
</form>

And in my Python code:

@route('/submit', method='POST')
def handle_submission():
    user_input = request.forms.get('user_name')
    # How do I include user_input in the email?
    response = requests.post(
        "https://api.mailgun.net/v3/yourdomain.com/messages",
        auth=("api", "your-api-key"),
        data={
            "from": "Your Site <[email protected]>",
            "to": "[email protected]",
            "subject": "New Form Submission",
            "text": "??? How to include form data here ???"
        }
    )
    return 'Form submitted!'

Can anyone help me figure out how to include the form data in the email body? Thanks in advance!

To incorporate form data into your email body, you can modify the ‘text’ field in your data dictionary. Here’s an approach:

@route('/submit', method='POST')
def handle_submission():
    user_name = request.forms.get('user_name')
    # Collect other form fields similarly
    
    email_body = f"New Form Submission\n\nName: {user_name}\n"
    # Add other form fields to email_body as needed
    
    response = requests.post(
        "https://api.mailgun.net/v3/yourdomain.com/messages",
        auth=("api", "your-api-key"),
        data={
            "from": "Your Site <[email protected]>",
            "to": "[email protected]",
            "subject": "New Form Submission",
            "text": email_body
        }
    )
    return 'Form submitted successfully!'

This method allows you to format the email body with all relevant form data before sending it via Mailgun.

hey jack, i’ve done this before! you’re close. just update the “text” field in your data dictionary:

"text": f"New submission from {user_input}"

this’ll include the user’s name in the email. you can add more form fields the same way. hope this helps!

I’ve tackled this issue before, and there’s a neat trick to make it work smoothly. Instead of just including the user’s name, you can create a more comprehensive email body that includes all form fields. Here’s what I’d suggest:

@route('/submit', method='POST')
def handle_submission():
    form_data = request.forms
    email_body = "New Form Submission\n\n"
    for field, value in form_data.items():
        email_body += f"{field.capitalize()}: {value}\n"

    response = requests.post(
        "https://api.mailgun.net/v3/yourdomain.com/messages",
        auth=("api", "your-api-key"),
        data={
            "from": "Your Site <[email protected]>",
            "to": "[email protected]",
            "subject": "New Form Submission",
            "text": email_body
        }
    )
    return 'Form submitted successfully!'

This approach dynamically includes all form fields in the email, so you don’t have to manually specify each one. It’s particularly useful when you have multiple form fields or if you plan to add more in the future.