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