def send_email(self, emails, subject, text_content=None, html_content=None, main_content=None):
        """
        @subject, @text_content and @html_content are intuitive.
        @to must have be a string (email) or a list of strings (emails).
        @main_content must be 'text', 'html' or None. If None, it will be set to 'html' if html_content if provided, 'text' in other case.
        Returns True if success, False otherwise.
        """
        if USING_CELERY:
            from email_manager.tasks import send_email

            send_email.delay(emails, subject, text_content, html_content, main_content)
            return None
        try:
            if not emails:
                return None
            if isinstance(emails, str):
                emails = [emails]
            to = [emails[0]]
            bcc = emails[1:]  # used for network optimization
            formatted_subject = "%s %s" % (settings.EMAIL_SUBJECT_PREFIX, subject)
            msg = EmailMultiAlternatives(formatted_subject, text_content, to=to, bcc=bcc)
            if html_content:
                msg.attach_alternative(html_content, "text/html")
                if main_content == "html" or not main_content:
                    msg.content_subtype = "html"
                # else default is plain/text
            msg.send(fail_silently=False)
            for email in emails:
                EmailLog.create_log(email, subject)
            return True
        except SMTPException as e:
            EmailLog.create_log(email, subject, success=False, error=str(e))
            return False
 def send_email(self,
                emails,
                subject,
                text_content=None,
                html_content=None,
                main_content=None):
     """
     @subject, @text_content and @html_content are intuitive.
     @to must have be a string (email) or a list of strings (emails).
     @main_content must be 'text', 'html' or None. If None, it will be set to 'html' if html_content if provided, 'text' in other case.
     Returns True if success, False otherwise.
     """
     if USING_CELERY:
         from email_manager.tasks import send_email
         send_email.delay(emails, subject, text_content, html_content,
                          main_content)
         return None
     try:
         if not emails:
             return None
         if isinstance(emails, str):
             emails = [emails]
         to = [emails[0]]
         bcc = emails[1:]  # used for network optimization
         formatted_subject = '%s %s' % (settings.EMAIL_SUBJECT_PREFIX,
                                        subject)
         msg = EmailMultiAlternatives(formatted_subject,
                                      text_content,
                                      to=to,
                                      bcc=bcc)
         if html_content:
             msg.attach_alternative(html_content, "text/html")
             if main_content == 'html' or not main_content:
                 msg.content_subtype = "html"
             # else default is plain/text
         msg.send(fail_silently=False)
         for email in emails:
             EmailLog.create_log(email, subject)
         return True
     except SMTPException as e:
         EmailLog.create_log(email, subject, success=False, error=str(e))
         return False
 def test_must_truncate_long_subjects(self):
     log = EmailLog.create_log('*****@*****.**', 'x' * 101, success=True, error=None)
     self.assertEquals('x' * 100, log.subject)
 def test_must_truncate_long_error_messages(self):
     log = EmailLog.create_log('*****@*****.**', 'x', success=False, error='x' * 256)
     self.assertEquals('x' * 255, log.error)
 def test_must_not_ignore_error_in_case_of_error(self):
     log = EmailLog.create_log('*****@*****.**', 'x', success=False, error='x')
     self.assertEquals('x', log.error)
 def test_must_ignore_error_in_case_of_success(self):
     log = EmailLog.create_log('*****@*****.**', 'x', success=True, error='x')
     self.assertEquals(None, log.error)
 def test_must_truncate_long_subjects(self):
     log = EmailLog.create_log('*****@*****.**',
                               'x' * 101,
                               success=True,
                               error=None)
     self.assertEquals('x' * 100, log.subject)
 def test_must_truncate_long_error_messages(self):
     log = EmailLog.create_log('*****@*****.**',
                               'x',
                               success=False,
                               error='x' * 256)
     self.assertEquals('x' * 255, log.error)
 def test_must_not_ignore_error_in_case_of_error(self):
     log = EmailLog.create_log('*****@*****.**', 'x', success=False, error='x')
     self.assertEquals('x', log.error)
 def test_must_ignore_error_in_case_of_success(self):
     log = EmailLog.create_log('*****@*****.**', 'x', success=True, error='x')
     self.assertEquals(None, log.error)