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

2024-09-29 19:20:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我了解使用Python和来自我的站点的请求通过mailgunapi发送消息的基本原理,一切都很好。我想从一个HTML表单附加数据使用请求.forms.get(''),但无法找到使其工作的语法。下面的链接正是我需要做的,除了用Python而不是PHP。在

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

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

HTML表单(其中的一部分用于理解要点)

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

路线(其中的一部分用于将点穿过)

^{pr2}$

谢谢你


Tags: 数据namesendhttp消息表单get站点
2条回答

您可以使用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 '''

相关问题 更多 >

    热门问题