使用Mautic API,如何在创建电子邮件时发送参数“list”?

2024-09-28 21:26:59 发布

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

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

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

列出应添加到段电子邮件的段ID数组

如何使用Python通过httppost发送参数列表,以便Mautic API不受影响?在

这将在Mautic中创建一个“template”(默认)类型的电子邮件。。。在

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

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

为此,必须指定每个列表ID。 列表是莫里斯语中的片段。。。。 我有一个ID为7的段!在

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

^{pr2}$

我试过很多方法。。。我总是得到错误的答案:

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的修改版本


Tags: httpsapiid类型列表参数电子邮件email
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

秘密就在 数据=json.dumps文件(params),它将参数转换为原始json

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

Array of segment IDs which should be added to the segment email

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

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

使用Python中的请求,我生成了一个url安全的有效负载字符串,其外观类似于以下截取的内容,以便将列表Id传递给分段电子邮件:

lists%5B%5D=7

等于

^{pr2}$

用简单的字体。所以你必须把[]直接放在键名后面。在

为了创建一个电子邮件列表(段电子邮件)并附加一个段,在邮递员的帮助下生成了以下代码:

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

希望这有帮助!在

相关问题 更多 >