New layout pages.
[wolnelektury.git] / src / newsletter / subscribe.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 import requests
5 from django.conf import settings
6
7
8 def subscribe(email, newsletter):
9     list_id = newsletter.phplist_id
10     data = {
11         "email": email,
12         "emailconfirm": email,
13         f"list[{list_id}]": "signup",
14         "htmlemail": 1,
15         "subscribe": "Subscribe",
16     }
17     if settings.NEWSLETTER_PHPLIST_SUBSCRIBE_URL:
18         response = requests.post(
19             settings.NEWSLETTER_PHPLIST_SUBSCRIBE_URL,
20             data=data,
21         )
22         response.raise_for_status()
23     else:
24         print("Newsletter not configured, "
25             "NEWSLETTER_PHPLIST_SUBSCRIBE_URL not set. "
26             f"Trying to subscribe email: {email} to newsletter: {list_id}."
27         )
28