【问题标题】:Sending an Email with Form Data via Mailgun and Python通过 Mailgun 和 Python 发送带有表单数据的电子邮件
【发布时间】:2016-04-25 04:54:21
【问题描述】:

我了解使用 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 '''

谢谢

【问题讨论】:

    标签: python mailgun


    【解决方案1】:

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

    【讨论】:

    • 您好 User312016。我已经导入了请求,但也许我没有完全使用它?您能否举例说明我如何从名字字段发送数据,例如在路由中?谢谢
    • @VictorEliasRodriguez 我修改了我的示例。
    • 您好 user312016。我明白你现在做了什么。它只需创建一个变量 = request.forms.get('name') 并在“文本”区域中使用它。谢谢!
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-06
      • 2022-10-01
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-13
      相关资源
      最近更新 更多