3

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

Alex Benfica
  • 408
  • 5
  • 17

3 Answers3

3

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!

d0utone
  • 180
  • 11
  • You nailed it! emailData['lists[]'] = '7' I have to refactor this now almost 1 year later and I found my own question with your answer when I faced the same issue! – Alex Benfica May 31 '18 at 21:21
0

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>'
}      
etemple1
  • 1,748
  • 1
  • 11
  • 13
  • Hi! I tried this as well before and I got the same error. – Alex Benfica May 31 '18 at 21:19
  • Okay, I wanted to comment on @etemple1 comment, but apparently my reputation isn't good enough to comment, what you need to do it provide an array of segments, and segment id should be integer in my opinion, so perhaps pass it as [1,2,3] or [1]. I am sure not because i have created email but sure because i have used similar pattern when changing Lead's segment. – Mayank Tiwari Aug 02 '19 at 16:21
0

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