通过 Mailgun 和 Python 发送带有表单数据的电子邮件

Sending an Email with Form Data via Mailgun and Python

我了解使用 Python 通过 Mailgun API 发送消息的基础知识以及来自我的站点的请求,一切正常。我想使用 request.forms.get('') 从 HTML 表单附加数据,但无法弄清楚使其工作的语法。下面的 link 正是我需要做的,除了 Python 而不是 PHP.

http://www.formget.com/mailgun-send-email/

如何通过 Mailgun 发送以下表单数据?

HTML 表格(其中的一部分用于说明要点)

<form action="/send" method="post">
<input name="name" placeholder="Name">
...
<button> ...

ROUTE(其中的一部分用于说明要点)

@route('/send', method='POST')
def send_simple_message():
variable_I_need_to_send = request.forms.get('firstname')
...
data={...",
        "to": "MyName <myname@gmail.com>",
        "subject": "Website Info Request",
        "text": "Testing some Mailgun awesomness!",
        "html": **variable_I_need_to_send**})
    return '''

谢谢

您可以使用 requests 库:

import requests

@route('/send/<firstname>', method='POST')
def send_simple_message(first_name):
    ...
    data={...",
            "from": "{} <address>".format(first_name),
            "to": "MyName <myname@gmail.com>",
            "subject": "Website Info Request",
            "text": "Testing some Mailgun awesomness!",
            "html": '**I need to include form data here**'})

    requests.post('http://api_url/', data=data)
    return 

这对我有用,但不确定这是否是正确的处理方式:

@route('/send', method='POST')
def send_simple_message():
    subject = request.forms.get('name')
    item1 =  request.forms.get('emailaddress')
    item2 =  request.forms.get('phone')
    item3 =  request.forms.get('url')
    item4 =  request.forms.get('about')
    text = item1 + " " + item2 + " " + item3 + " " + item4
    ...
        "to": "Vic <myemail@gmail.com>",
        "subject": subject,
        "html": text})
    return '''