2

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

如果不指定参数列表,我将无法创建电子邮件。列表参数指定如下:

列表数组 应添加到段电子邮件的段 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>'
}       

但我需要的是创建一个“列表”类型的电子邮件。

为此,必须指定每个列表 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的修改版本

4

3 回答 3

2

使用 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 lib):

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

希望这可以帮助!

于 2017-06-21T11:10:52.623 回答
0

根据您链接到的 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>'
}      
于 2017-05-24T13:42:40.170 回答
0

您需要将数据作为原始 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

于 2019-07-31T21:39:07.873 回答