【问题标题】:Using Mautic API, how do I send the parameter "lists" when creating an email?使用 Mautic API,如何在创建电子邮件时发送参数“列表”?
【发布时间】:2017-05-23 22:21:57
【问题描述】:

使用 Mautic API 创建电子邮件的文档是: https://developer.mautic.org/#create-email

如果不指定参数 lists,我将无法创建电子邮件。 lists 参数是这样指定的:

lists array 应该添加到段电子邮件的段 ID 数组

如何使用 Python 通过 HTTP post 发送参数列表,以便 Mautic API 可以识别它?

这会在 Mautic 中创建一个类型为“模板”(默认)的电子邮件...

emailData = {    
    'name': 'Email-teste',
    'subject': 'Assunto teste',
    'isPublished': '1',
    'language': 'pt_BR',`enter code here`
    'customHtml' : '<strong>html do email<strong>'
}       

但我需要创建一个类型为“list”的电子邮件。

为此,必须指定每个列表 ID。 列表是 Mautic 中的细分...... 我有一个 ID 为 7 的片段!

如何使用 POST(Python 请求)将分段 ID 发送到 Mautic API?

emailData = {    
    'name': 'Email-teste',
    'subject': 'Assunto teste',
    'emailType': 'list',
    'lists': '7',    
    'isPublished': '1',
    'language': 'pt_BR',
    'customHtml' : '<strong>html do email<strong>'
}       

我尝试了很多方法......但我总是得到错误:

u'errors': [{u'code': 400,
              u'details': {u'lists': [u'This value is not valid.']},
              u'message': u'lists: This value is not valid.'}]}

我确定我有一个 ID 为 7 的段,正如我在 Mautic 界面中看到的那样。

我使用的是https://github.com/divio/python-mautic的修改版

【问题讨论】:

    标签: python http-post python-requests mautic


    【解决方案1】:

    使用 Python 中的请求,我生成了一个 url 安全的 Payload 字符串,如下所示,以便将列表 ID 传递给分段电子邮件:

    lists%5B%5D=7
    

    等于

    lists[]=7
    

    在纯脚本中。所以你必须把 [] 直接放在键名后面。

    为了创建附有分段的电子邮件列表(分段电子邮件),在 Postman 的帮助下生成了以下代码:

    import requests
    
    url = "https://yourmauticUrl"
    
    payload = "customHtml=%3Ch1%3EHello%20World%3C%2Fh1%3E&name=helloworld&emailType=list&lists%5B%5D=7"
    headers = {
        'authorization': "your basic auth string",
        'content-type': "application/x-www-form-urlencoded",
        'cache-control': "no-cache"
        }
    
    response = requests.request("PATCH", url, data=payload, headers=headers)
    
    print(response.text)
    

    看看你的特殊问题,我可以想象你的代码应该是这样的(虽然我不熟悉你的 python 库):

    emailData = {    
        'name': 'Email-teste',
        'subject': 'Assunto teste',
        'emailType': 'list',
        'lists[]': '7',    
        'isPublished': '1',
        'language': 'pt_BR',
        'customHtml' : '<strong>html do email<strong>'
    }  
    

    希望这会有所帮助!

    【讨论】:

    • 你成功了! emailData['lists[]'] = '7' 将近 1 年后我现在必须重构它,当我遇到同样的问题时,我发现我自己的问题和你的答案!
    【解决方案2】:

    根据您链接到的 API 文档,lists 必须是:

    应添加到细分电子邮件中的细分 ID 数组

    但是,您没有在列表(数组)中发送lists 的值。相反,您应该尝试:

    emailData = {    
        'name': 'Email-teste',
        'subject': 'Assunto teste',
        'emailType': 'list',
        'lists': ['7'],    
        'isPublished': '1',
        'language': 'pt_BR',
        'customHtml' : '<strong>html do email<strong>'
    }      
    

    【讨论】:

    • 嗨!我之前也试过这个,我得到了同样的错误。
    • 好的,我想对@etemple1 评论发表评论,但显然我的声誉不足以发表评论,您需要做的是提供一个段数组,段 id 应该是整数我的意见,所以也许将其传递为 [1,2,3] 或 [1]。我确定不是因为我创建了电子邮件,而是因为我在更改 Lead 的细分时使用了类似的模式。
    【解决方案3】:

    您需要将数据作为原始 json 发送,这是请求的示例:

    def create_contact_mautic(email, firstname, lastname):
        params = {"email": email}
        params.update({"firstname": firstname})
        params.update({"lastname": lastname})
        url = '<your mautic url>/api/contacts/new'
        response = requests.request('POST', url, data=json.dumps(params), headers=headers, auth=('<your login>','<your password>'))
        return response.text
    
    

    秘密在 data=json.dumps(params),将您的参数转换为原始 json

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多