Найти - Пользователи
Полная версия: Отправка email сообщений через одно соединение
Начало » Django » Отправка email сообщений через одно соединение
1
andreiru
Здравствуйте!

Написал класс для отправки нескольких email сообщений через одно SMTP соединение, подскажите пожалуйста все ли я правильно сделал или можно лучше ?


from django.core.mail import get_connection, EmailMultiAlternatives
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.template import loader, Context
from django.utils.html import strip_tags
class Mail:
    def __init__(self):
        self.connection = get_connection()
        self.connection.open()
    def __del__(self):
        self.connection.close()
    def send(self, to, subject, template, context={}, files=None, fail_silently=False):
        if type(to) != list: to = [to, ]
        ctx = Context(context)
        tmpl = loader.get_template(template)
        html_part = tmpl.render(ctx)
        text_part = strip_tags(html_part)
        msg = EmailMultiAlternatives(subject, text_part, settings.EMAIL_HOST_USER, to, connection=self.connection)
        msg.attach_alternative(html_part, 'text/html')
        if files:
            if type(files) != list:
                files = [files, ]
            for file in files:
                msg.attach_file(file)
        return msg.send(fail_silently)
mail = Mail()


from project.utils import mail:
for x in xrange(10):
    mail.send(['user@ya.ru'], 'test %s' % x, 'base.html')
   
PanovSergey
Мыло штука не надежная и медленная. Отправку сообщений в другой поток а лучше в несколько.
Singularity
Потоки не пройдут

Надо что-то типа celery
https://github.com/ui/django-post_office
https://pypi.python.org/pypi/django-celery-email
https://github.com/ui/django-rq
andreiru
Singularity
Потоки не пройдут Надо что-то типа celeryhttps://github.com/ui/django-post_officehttps://pypi.python.org/pypi/django-celery-emailhttps://github.com/ui/django-rq

Спасибо, решил попробовать post_office !
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB