Home > Mobile >  Using Mautic API, how do I send the parameter "lists" when creating an email?
Using Mautic API, how do I send the parameter "lists" when creating an email?

Time:01-06

The documentation for creating emails using Mautic API is: https://developer.mautic.org/#create-email

I can not create an email without specify the parameter lists. The lists parameter is specified like this:

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

How can I send the parameter lists via HTTP post using Python so that Mautic API can undestand it?

This creates a email of type "template" (default) in Mautic...

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

But what I need is to create an email of type "list".

For that, it is mandatory to specify each list ids. Lists are the segments in Mautic.... I have a segment with ID 7!

How can I send the segments IDs to Mautic API using POST (Python requests)?

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

I tried many ways... and I always get the errror:

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

I am sure I have a segment with ID 7, as I can see in Mautic interface.

I am using a modified version of https://github.com/divio/python-mautic

CodePudding user response:

Using requests in Python, I generated an url safe Payload string looking like the following snipped in order to pass the list Id to a segment email:

lists[]=7

equals

lists[]=7

in plain script. So you have to put the [] directly behind the key-name.

In order to create an email as list (segment email) with a segment attached to it generated the following code with the help of Postman:

import requests

url = "https://yourmauticUrl"

payload = "customHtml=

Hello World

&name=helloworld&emailType=list&lists[]=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)

Looking at your particular problem, I could imagine that your code should look like this (although I am not familiar with thy python lib):

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

Hope this helps!

CodePudding user response:

Per the API docs you linked to, lists need to be:

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

But, you aren't sending the value for lists in a list (array). Instead, you should try:

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

CodePudding user response:

you need to send the data as raw json , here's the example of the request:

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

the secret is at data=json.dumps(params), that transforms your parameters into raw json

  • Related