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

时间:2016-04-25 04:54:21

标签: python mailgun

我理解使用Python通过Mailgun API发送消息的基本知识以及来自我网站的请求,并且一切正常。我想使用request.forms.get('')附加HTML表单中的数据,但无法找出使其工作的语法。下面的链接正是我需要做的,除了Python而不是PHP。

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

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

HTML FORM(部分内容可以解决问题)

<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 '''

谢谢

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

这对我有用,但不确定它是不是正确的做事方式:

@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 '''