Alex Benfica
Alex Benfica

Reputation: 408

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

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

Upvotes: 3

Views: 2210

Answers (3)

Igor Marinelli
Igor Marinelli

Reputation: 1

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

Upvotes: 0

d0utone
d0utone

Reputation: 300

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%5B%5D=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=%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)

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!

Upvotes: 4

etemple1
etemple1

Reputation: 1788

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

Upvotes: 0

Related Questions