skoudoro / mailerlite-api-python Goto Github PK
View Code? Open in Web Editor NEWPython wrapper for Mailerlite API v2
License: Other
Python wrapper for Mailerlite API v2
License: Other
When trying to add a subscriber to a group, if you don't use as_json=True then it throws an exception because it tries and convert the full response to a Subscriber when the response returned by MailerLite is different dict (imported, updated, unchanged, errors
Here is the code that fails:
data = {
'email': request.user.email,
}
result = api.groups.add_subscribers(group_id, [data])
And the exception I get
File ".../.venv/lib/python3.8/site-packages/mailerlite/group.py", line 206, in add_subscribers
return Subscriber(**res_json)
I try to use the script from README, it seems that Mailerlite receives it and starts to build a campaign, but nothing happens. There is a 'sending' status in the admin panel, but still no mail on the inbox. Am I missing something?
from mailerlite import MailerLiteApi
api = MailerLiteApi('mailerlite-api-key')
data = {"subject": "Regular campaign subject",
"name": "Regular campaign name",
"groups": [GroupID],
"type": "regular"}
_, res = api.campaigns.create(data)
campaign_id = res['id']
html = '<head></head><body><h1>Your Title</h1><p>Your Content</p><p><small>'
html += '<a href=\"{$unsubscribe}\">Unsubscribe</a></small></p></body>'
plain = "Your email client does not support HTML emails. "
plain += "Open newsletter here: {$url}. If you do not want"
plain += " to receive emails from us, click here: {$unsubscribe}"
api.campaigns.update(campaign_id, html=html, plain=plain)
api.campaigns.send(campaign_id)
When I try to send a campaign using an example in README file I got an error AttributeError: 'MailerLiteApi' object has no attribute 'campaign'. I think it suppose to be api.campaigns.create, api.campaigns.update, api.campaigns.send
Hi,
first of all, thank you for your work. It should be useful for my startup.
Do you have any documentation about the group feature? Or maybe you still didn't develop anything regarding the groups?
Let me explain you my needs, you may redirect me to the good solution.
I am developing a saas business.
When users didn't use at all my software, I would like to send them a series of mails. So I should create an automation series of mail for a specific group. Let's call this group "NON INSTALL GROUP".
And then, when my software will detect a user didn't use it at all, it will add this user in this specific group : "NON INSTALL GROUP"
If the user used my software, it will check if user is in this group and remove him/her.
When users didn't use since a long time my software, I would like to send them a series of mails. So I should create an automation series of mail for a specific group. Let's call this group "NOT USED SINCE LONG TIME GROUP".
And then, when my software will detect a user didn't use since a long time, it will add this user in this specific group : "NOT USED SINCE LONG TIME GROUP"
If the user used my software, it will check if user is in this group and remove him/her.
I think you get the idea of my specific needs. It is important for the retention of my saas. And RETENTION is king in SAAS business. It is very important.
Maybe there is another way to do what I need?
Kind regards
I'm using the API to fetch subscribers from Mailerlite via Python. Specifically, I'm using below call:
api.subscribers.all(limit=2000, as_json=True)
This results in a list of subscribers with below JSON details per subscriber
{'id': 1261159647788771324, 'name': '', 'email': '[email protected]', 'sent': 0, 'opened': 0, 'opened_rate': 0, 'clicked': 0, 'clicked_rate': 0, 'type': 'active', 'country_id': 49, 'signup_ip': '88.23.20.209', 'signup_timestamp': '2022-04-05 22:39:05', 'confirmation_ip': '88.23.20.209', 'confirmation_timestamp': '2022-04-05 22:39:05', 'fields': [{'key': 'email', 'value': '[email protected]', 'type': 'TEXT'}, {'key': 'name', 'value': '', 'type': 'TEXT'}, {'key': 'last_name', 'value': '', 'type': 'TEXT'}, {'key': 'company', 'value': '', 'type': 'TEXT'}, {'key': 'country', 'value': '', 'type': 'TEXT'}, {'key': 'city', 'value': '', 'type': 'TEXT'}, {'key': 'phone', 'value': '', 'type': 'TEXT'}, {'key': 'state', 'value': '', 'type': 'TEXT'}, {'key': 'zip', 'value': '', 'type': 'TEXT'}], 'webform_subscribe_date': None, 'date_subscribe': None, 'date_unsubscribe': None, 'date_created': '2022-04-05 22:39:05', 'date_updated': '2022-04-05 22:39:05', 'user_agent': None}
I would like to identify the group_id of the subscribers and I would have expected this to be part of the subscriber JSON - but that does not seem to be the case. Any idea what might be missing in my call to get this - or what would be the best way to extract it?
Hi! It seems like a new version is needed for the changes from #32 to get into the public pip package.
Mind revving the version please?
I found out that the activity endpoint of the subscribers API from mailerlite has as well a limit and offset parameter.
This is not specified in the mailerlite API documentation.
https://developers.mailerlite.com/reference/activity-of-single-subscriber
Just calling the activity of the subscriber endpoint (with this library) doesn't return the full activity history of the subscriber.
When I call the API directly without this mailerlite-api-python wrapper library, and add limit and offset, I'm able to get the full history.
import requests
url = "https://api.mailerlite.com/api/v2/subscribers/987654321/activity?offset=0&limit=1000"
headers = {
"Accept": "application/json",
"X-MailerLite-ApiDocs": "true",
"X-MailerLite-ApiKey": "....mykey..."
}
response = requests.request("GET", url, headers=headers)
print(response.text)
Need to update unittest for all modules. every module needs its own PR
ok, I might have found a solution using make_dataclasses
. e.g: https://stackoverflow.com/a/52536584/17925831
I will try to add that this weekend
Originally posted by @skoudoro in #43 (comment)
When I find the time, it would be good to have a GitHub action for publishing release after pushing a tag
It should facilitate maintenance.
Installed 0.10.0 via pip [venv], still receiving:
from mailerlite import MailerLiteApi
ModuleNotFoundError: No module named 'mailerlite'
py3.8
Using Github action, Let's run the tests 2 times per month to detect any change in the mailerlite api. Currently, it has been 2 months without any run.
Hi,
I'm trying to pull out my subscribers as follows:
from mailerlite import MailerLiteApi
def mailerlite_loader(api_key):
api = MailerLiteApi(api_key)
subs = api.subscribers.all()
email_subs = []
for i in range(len(subs)):
email_subs.append(subs[i].email)
return email_subs
The above, however, only returns 100 subscribers (there are many more). Is there a way to get all the subscribers, either via one call or via some form of looping?
Thanks for making this library!
FWIW, I think it's good Python practice to not execute remote calls upon instantiating a new object. In this case, creating an API object will run 7 identical calls to /api/v2/stats
via client.check_headers()
.
I got this error creating new subscribers.
return Subscriber(**res_json) TypeError: <lambda>() got an unexpected keyword argument 'webform_subscribe_date'
mailerlite/group.py in add_single_subscriber at line 248
I checked the docs right now and nothing about this. I would say they removed the webform_subscribe_date attr
Description
I am trying to extract all subscribers from my mailerlite account through
Platform Ubuntu 20.10
Python 3.8.10
Observed behavior:
The python script failed with webform_subscribe_date missing.
This : TypeError: new() got an unexpected keyword argument 'webform_subscribe_date'
Expected behavior:
The python layer returns all registered and active user accounts .
Code for creating campaigns and sending mail by running campaigns.
from mailerlite import MailerLiteApi
from pprint import pprint
import time
api = MailerLiteApi('******************')
data = {"subject": "Regular campaign subject",
"name": "Regular campaign name",
"groups": [111509512],
"type": "regular"}
_, res = api.campaigns.create(data)
campaign_id = res['id']
html = '<head></head><body><h1>Your Title</h1><p>Your Content</p><p><small>'
html += '<a href=\"{$unsubscribe}\">Unsubscribe</a></small></p></body>'
plain = "Your email client does not support HTML emails. "
plain += "Open newsletter here: {$url}. If you do not want"
plain += " to receive emails from us, click here: {$unsubscribe}"
api.campaigns.update(campaign_id, html=html, plain=plain)
api.campaigns.send(campaign_id)
Running this code creates a campaign and moves the campaign to the mailerlite outbox. After 1 minute, it says it has been sent and goes to Sent. However, the mail is not actually sent, and when I click on the campaign in Sent, it says Sending...
Am I the only one like this? I would appreciate it if you could tell me the solution.
Hi,
Love this package, its very useful so thanks for your hard work.
I was looking thru the API this package provides, but cannot see a way to fetch a subscribers status in a group (eg: active, unsubscribed, unconfirmed, bounced, junk). Just wondering if you know of how to do this, looking at both V1 & V2 API docs, its very unclear if this is even possible from mailerlite's API.
Cheers
The current performance is ok but Faster is better.
For learning purposes and to improve the performance of huge mailing lists, it would be good to use aiohttp
and asyncio
.
For more information, check here. Need to handle multiple users and not only one.
Need to add campaign sent attributes in a Campaign
object
response example of the campaign sent:
{'id': 42706640, 'total_recipients': 0, 'type': 'regular', 'extra_type': None, 'name': 'Regular campaign name', 'status': 'draft', 'step': 3, 'send_rule': None, 'opened': {'count': None, 'rate': None}, 'clicked': {'count': None, 'rate': None}, 'followup_info': {'send_type': 'regular', 'followup_send_type': None}, 'ab_info': {'send_type': 'regular', 'ab_win_type': None}, 'date_created': '2021-10-18 14:08:49', 'date_send': '2021-10-18 14:08:00', 'date_end': None, 'mail': {'id': 45258518, 'date': '2021-10-18 14:08:49', 'subject': 'Regular campaign subject', 'code': 'v4u1v8', 'from': '[email protected]', 'from_name': 'Demo API v2', 'url': 'regular-campaign-subject-45258518', 'send_date': '2021-10-18 14:08:00', 'host': 'mailerlite.com', 'type': 'template', 'screenshot': 'https://static.mailerlite.com/images/newsletter_screenshot2.png', 'screenshot_big': 'https://static.mailerlite.com/images/newsletter_screenshot2.png', 'updated': '2021-10-18 11:08:51', 'language': {'code': 'en', 'title': 'English'}, 'groups': [{'id': 2984475, 'name': 'Asmeniniai', 'total': 6, 'active': 6, 'unsubscribed': 0, 'bounced': 0, 'unconfirmed': 0, 'junk': 0, 'sent': 241, 'opened': 74, 'clicked': 40, 'ordering': 34, 'updating': 0, 'user_id': 0, 'date': '2015-12-16 15:43:46', 'updated': '2020-07-07 16:44:57'}, {'id': 3237221, 'name': 'Gumroad', 'total': 0, 'active': 0, 'unsubscribed': 0, 'bounced': 0, 'unconfirmed': 0, 'junk': 0, 'sent': 83, 'opened': 52, 'clicked': 24, 'ordering': 30, 'updating': 0, 'user_id': 0, 'date': '2016-02-03 10:33:01', 'updated': '2020-07-07 16:44:57'}]}, 'mails': [{'id': 45258518, 'date': '2021-10-18 14:08:49', 'subject': 'Regular campaign subject', 'code': 'v4u1v8', 'from': '[email protected]', 'from_name': 'Demo API v2', 'url': 'regular-campaign-subject-45258518', 'send_date': '2021-10-18 14:08:00', 'host': 'mailerlite.com', 'type': 'template', 'screenshot': 'https://static.mailerlite.com/images/newsletter_screenshot2.png', 'screenshot_big': 'https://static.mailerlite.com/images/newsletter_screenshot2.png', 'updated': '2021-10-18 11:08:53'}], 'includes_survey': False, 'has_ecommerce': False, 'report_date': None}
I think mailerlite has updated some stuff on their endpoints.
The function add_single_subscriber on group module is failing because this new parameter(s). For instance "user_agent".
Specifically here:
mailerlite-api-python/mailerlite/group.py
Line 248 in e1aa102
This may affect other parts. I didn't test it.
I can take a look later on this week but feel free to fix this. I'll need this soon.
Cheers!
It would be good to use an environment variable for the API keys. Offer this option
It seems like the URL set in MAILERLITE_API_V2_URL is no longer correct: https://api.mailerlite.com/api/v2/
Mailerlite API documentation suggests using https://connect.mailerlite.com
I am having issues with creating a campaign, and I believe this is the cause. Perhaps MAILERLITE_API_V2_URL should be updated?
Hi, I've tried your example on how to update a subscriber profile, but I realized it's not possible to use the email field.
Reading MailerLite's documentation, they suggest creating a new subscriber when the email changes.
Is that right?
data = {'name': 'John', 'email': '[email protected]', 'fields': {'company': 'MailerLite'} } api.subscribers.update(data, id='1343965485')
Example with api.groups.add_single_subscriber. This method has't arg subscriber_data, but has subscribers_data.
I am trying to update several hundred contacts which fails because of the rate limit. It would be cool if this library would recognize the rate limit headers and delayed accordingly: https://developers.mailerlite.com/docs/#rate-limits
Hello~
Thanks so much for the library I'm looking at the mailer docs and I think the endpoint might have updated to add a single subscriber. It looks like we pass in the group_id and subscriber_id instead of the subscriber data. Where would be a good place to update in the mailerlite-api-python lib files?
Can get and search subscriber by email, but via postman it works.
In [25]: mailerLiteApi.subscribers.get(email='[email protected]')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-25-ba3f407787d0> in <module>
----> 1 mailerLiteApi.subscribers.get(email='[email protected]')
~/.local/lib/python3.8/site-packages/mailerlite/subscriber.py in get(self, as_json, **identifier)
262 res_json['fields'] = [Field(**res) for res in res_json['fields']]
263
--> 264 return Subscriber(**res_json)
265
266 def delete(self, subscriber_id):
TypeError: __new__() got an unexpected keyword argument 'groups'
In [28]: mailerLiteApi.subscribers.search(search='[email protected]')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-28-753a59c1d509> in <module>
----> 1 mailerLiteApi.subscribers.search(search='[email protected]')
~/.local/lib/python3.8/site-packages/mailerlite/subscriber.py in search(self, search, limit, offset, minimized, as_json)
320 res['fields'] = [Field(**field) for field in res['fields']]
321
--> 322 all_subscribers = [Subscriber(**res) for res in res_json]
323 return all_subscribers
324
~/.local/lib/python3.8/site-packages/mailerlite/subscriber.py in <listcomp>(.0)
320 res['fields'] = [Field(**field) for field in res['fields']]
321
--> 322 all_subscribers = [Subscriber(**res) for res in res_json]
323 return all_subscribers
324
TypeError: __new__() got an unexpected keyword argument 'groups'
via postman is ok https://connect.mailerlite.com/api/subscribers/[email protected]
API https://developers.mailerlite.com/docs/subscribers.html#fetch-a-subscriber
Recently, Mailerlite API has been changed and I got this exception:
File "src/mailerlite-api-python/mailerlite/subscriber.py", line 409, in <listcomp>
all_activities = [Activity(**res) for res in res_json]
TypeError: __new__() got an unexpected keyword argument 'campaign_name'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.