A simple Bash script for exporting all contacts from a specific Mautic segment to CSV using the Mautic REST API.
The script automatically works through the complete segment, so there is no need to manually specify a contact limit or page size.
The script requires:
- Bash
curljq- Access to the Mautic REST API
- Mautic credentials with permission to read contacts
Edit the configuration values at the top of mtc-export.sh:
MAUTIC="https://engage.yourdomain.com"
USER="user"
PASS="pass"
SEGMENT="segment-alias"Set MAUTIC to the base URL of your Mautic installation.
MAUTIC="https://engage.example.com"Do not include /api at the end.
Set USER and PASS to credentials that can access the Mautic API.
USER="api-user"
PASS="password"SEGMENT must contain the segment alias, not the segment name.
For example, if the segment is named:
Monthly Newsletter Subscribers
and its alias is:
monthly-newsletter
use:
SEGMENT="monthly-newsletter"Make the script executable:
chmod +x mtc-export.shThen run it:
./mtc-export.shThe script retrieves contacts from Mautic until the entire segment has been exported.
Progress is displayed while the export runs:
Exported 30 of 247 contacts...
Exported 60 of 247 contacts...
Exported 90 of 247 contacts...
When complete:
Done. Exported 247 contacts to contacts-monthly-newsletter.csv
The generated CSV file is named using the segment alias:
contacts-SEGMENT.csv
For example:
contacts-monthly-newsletter.csv
The CSV currently contains the following fields:
id
firstname
lastname
email
phone
company
Additional Mautic contact fields can be added by modifying the jq section of the script.
For example:
.contacts[] |
[
.id,
.fields.all.firstname,
.fields.all.lastname,
.fields.all.email,
.fields.all.phone,
.fields.all.company
] | @csvThe script queries:
/api/contacts
using Mautic's contact search syntax:
segment:segment-alias
It reads the number of contacts returned and Mautic's reported total, then advances the API start value until all contacts have been retrieved.
The current script stores the Mautic username and password directly in mtc-export.sh.
Do not commit real credentials to a public repository. For shared or production use, consider moving credentials to environment variables or another secure configuration method.