from celery import shared_task from django.core.mail import send_mail from notification.models import Subscriber from news import models from django.template.loader import render_to_string from django.conf import settings from smtplib import SMTPException @shared_task def send_email_with_news(news_ids): subscribers = Subscriber.objects.filter(state=Subscriber.USABLE) for s in subscribers: try: for n in news_ids: sent_news = models.News.objects.get(id=n) send_mail("G&M News", render_to_string(settings.NEWS_EMAIL_TEMPLATE, {"title": sent_news.title.get(s.locale), "subtitle": sent_news.subtitle.get(s.locale), "description": sent_news.description.get(s.locale), "code": s.update_code, "domain_uri": settings.DOMAIN_URI, "country_code": s.country_code}), settings.EMAIL_HOST_USER, [s.send_to], fail_silently=False) except SMTPException: continue