Exemple #1
0
 def clean(self):
     if self.to:
         clean_address_list(self.to, _('To'))
     if self.cc:
         clean_address_list(self.cc, _('Copy'))
     if self.bcc:
         clean_address_list(self.bcc, _('Blind copy'))
     if self.reply_to:
         clean_address_list(self.reply_to, _('Reply to'))
Exemple #2
0
    def send(self, context=None):
        """
        When sending an email a set of attributes will be required.

        The required attributes are mainly dictated by django.core.mail
        used to send mail:
        * Message or body.
        * Subject.
        * Recipients list or to.
        * From email

        :param context: A dictionary with context variables to be used with
                        the subject and the message.
        :return: A tuple (result, message) where result is a boolean indicating
                 if mail could be sent or not. An a message in case the mail
                 could not be sent the message will be the reason. This could
                 have future uses if logging is implemented.
        """
        subject = self.subject
        body = self.body
        if context is None:
            # Needed whe no context is received so no replacement is tried.
            pass
        elif not isinstance(context, dict):
            raise ValueError(
                _('The argument for send method must be a '
                  'mapping.'))
        else:
            subject = replace_context_variable(text=self.subject,
                                               context_variable=context)
            body = replace_context_variable(text=self.body,
                                            context_variable=context)
        msg = EmailMultiAlternatives(subject=subject,
                                     from_email=self.from_email,
                                     to=clean_address_list(self.to),
                                     cc=clean_address_list(self.cc),
                                     bcc=clean_address_list(self.bcc),
                                     reply_to=clean_address_list(
                                         self.reply_to))
        msg.body = body
        msg.attach_alternative(body, 'text/html')
        return msg.send()
 def test_receive_none_return_empty_list(self):
     result = clean_address_list(None)
     assert result == []
 def test_receive_empty_string_return_empty_list(self):
     result = clean_address_list('')
     assert result == []
 def test_receive_string_with_more_than_one_address_return_a_list(self):
     result = clean_address_list('[email protected], [email protected], [email protected]')
     assert result == ['*****@*****.**', '*****@*****.**', '*****@*****.**']
 def test_receive_list_with_one_email_return_same_list(self):
     result = clean_address_list([
         '*****@*****.**',
     ])
     assert result == ['*****@*****.**']
 def test_receive_string_with_one_email_return_a_list(self):
     result = clean_address_list('*****@*****.**')
     assert result == ['*****@*****.**']