|
15 | 15 | from openwisp_notifications import settings as app_settings
|
16 | 16 | from openwisp_notifications import types
|
17 | 17 | from openwisp_notifications.swapper import load_model, swapper_load_model
|
18 |
| -from openwisp_notifications.utils import send_notification_email |
| 18 | +from openwisp_notifications.utils import ( |
| 19 | + get_unsubscribe_url_email_footer, |
| 20 | + get_unsubscribe_url_for_user, |
| 21 | + send_notification_email, |
| 22 | +) |
19 | 23 | from openwisp_utils.admin_theme.email import send_email
|
20 | 24 | from openwisp_utils.tasks import OpenwispCeleryTask
|
21 | 25 |
|
@@ -272,33 +276,42 @@ def send_batched_email_notifications(instance_id):
|
272 | 276 | '%B %-d, %Y, %-I:%M %p %Z'
|
273 | 277 | )
|
274 | 278 |
|
275 |
| - context = { |
| 279 | + extra_context = { |
276 | 280 | 'notifications': unsent_notifications[:display_limit],
|
277 | 281 | 'notifications_count': notifications_count,
|
278 | 282 | 'site_name': current_site.name,
|
279 | 283 | 'start_time': start_time,
|
280 | 284 | }
|
281 | 285 |
|
282 |
| - extra_context = {} |
| 286 | + user = User.objects.get(id=instance_id) |
| 287 | + unsubscribe_url = get_unsubscribe_url_for_user(user) |
| 288 | + extra_context['footer'] = get_unsubscribe_url_email_footer(unsubscribe_url) |
| 289 | + |
283 | 290 | if notifications_count > display_limit:
|
284 |
| - extra_context = { |
285 |
| - 'call_to_action_url': f"https://{current_site.domain}/admin/#notifications", |
286 |
| - 'call_to_action_text': _('View all Notifications'), |
287 |
| - } |
288 |
| - context.update(extra_context) |
289 |
| - |
290 |
| - html_content = render_to_string('emails/batch_email.html', context) |
291 |
| - plain_text_content = render_to_string('emails/batch_email.txt', context) |
| 291 | + extra_context.update( |
| 292 | + { |
| 293 | + 'call_to_action_url': f"https://{current_site.domain}/admin/#notifications", |
| 294 | + 'call_to_action_text': _('View all Notifications'), |
| 295 | + } |
| 296 | + ) |
| 297 | + |
| 298 | + plain_text_content = render_to_string( |
| 299 | + 'openwisp_notifications/emails/batch_email.txt', extra_context |
| 300 | + ) |
292 | 301 | notifications_count = min(notifications_count, display_limit)
|
293 | 302 |
|
294 | 303 | send_email(
|
295 | 304 | subject=f'[{current_site.name}] {notifications_count} new notifications since {start_time}',
|
296 | 305 | body_text=plain_text_content,
|
297 |
| - body_html=html_content, |
| 306 | + body_html=True, |
298 | 307 | recipients=[email_id],
|
299 | 308 | extra_context=extra_context,
|
| 309 | + headers={ |
| 310 | + 'List-Unsubscribe-Post': 'List-Unsubscribe=One-Click', |
| 311 | + 'List-Unsubscribe': f'<{unsubscribe_url}>', |
| 312 | + }, |
| 313 | + html_email_template='openwisp_notifications/emails/batch_email.html', |
300 | 314 | )
|
301 | 315 |
|
302 | 316 | unsent_notifications_query.update(emailed=True)
|
303 |
| - Notification.objects.bulk_update(unsent_notifications_query, ['emailed']) |
304 | 317 | cache.delete(cache_key)
|
0 commit comments