Написал класс для отправки нескольких 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')