Exemplo n.º 1
0
    def to_message(self):
        """Returns the email as MIMEText object."""
        if not self.text or not self.subject or \
           not self.to or not self.from_addr:
            raise ValueError('Fill in mailing parameters first')

        msg = MIMEText(utf8(self.text))

        # really MIMEText is sucks, it does not override values on setitem,
        # it appends them. Remove some predefined fields
        del msg['Content-Type']
        del msg['Content-Transfer-Encoding']

        msg['From'] = text_type(self.from_addr)
        msg['To'] = text_type(self.to)
        msg['Subject'] = text_type(self.subject)
        msg['Content-Type'] = 'text/plain; charset=utf-8'
        msg['Content-Transfer-Encoding'] = '8bit'

        if self.cc:
            msg['Cc'] = text_type(self.cc)

        if self.reply_to:
            msg['Reply-To'] = text_type(self.reply_to)

        return msg
Exemplo n.º 2
0
Arquivo: smtp.py Projeto: TeZzo1/MQTT
 def send(self, message):
     """Send the message."""
     with self as con:
         message.from_addr = message.from_addr or self.default_sender
         con.sendmail(text_type(message.from_addr),
                      text_type(message.send_to),
                      message.format())
Exemplo n.º 3
0
Arquivo: mail.py Projeto: TeZzo1/MQTT
    def to_message(self):
        """Returns the email as MIMEText object."""
        if not self.text or not self.subject or \
           not self.to or not self.from_addr:
            raise ValueError('Fill in mailing parameters first')

        msg = MIMEText(utf8(self.text))

        # really MIMEText is sucks, it does not override values on setitem,
        # it appends them. Remove some predefined fields
        del msg['Content-Type']
        del msg['Content-Transfer-Encoding']

        msg['From'] = text_type(self.from_addr)
        msg['To'] = text_type(self.to)
        msg['Subject'] = text_type(self.subject)
        msg['Content-Type'] = 'text/plain; charset=utf-8'
        msg['Content-Transfer-Encoding'] = '8bit'

        if self.cc:
            msg['Cc'] = text_type(self.cc)

        if self.reply_to:
            msg['Reply-To'] = text_type(self.reply_to)

        return msg
Exemplo n.º 4
0
 def __eq__(self, obj):
     if isinstance(obj, Address):
         return text_type(self) == text_type(obj)
     elif isinstance(obj, string_types):
         return text_type(self) == obj
     elif isinstance(obj, (list, tuple)):
         return text_type(self) == Address(obj)
     raise NotImplementedError('Unable to compare Address instance against '
                               '{} instance'.format(type(obj)))
Exemplo n.º 5
0
Arquivo: mail.py Projeto: TeZzo1/MQTT
 def __eq__(self, obj):
     if isinstance(obj, Address):
         return text_type(self) == text_type(obj)
     elif isinstance(obj, string_types):
         return text_type(self) == obj
     elif isinstance(obj, (list, tuple)):
         return text_type(self) == Address(obj)
     raise NotImplementedError(
         'Unable to compare Address instance against '
         '{} instance'.format(type(obj))
     )
Exemplo n.º 6
0
 def test_mail_contains_nonascii_characters(self, mail):
     mail.subject = u'Привет'
     assert 'Subject: =?utf-8?b?0J/RgNC40LLQtdGC?=' in text_type(mail)
     mail.from_addr = (u'Álice', u'á[email protected]')
     assert 'From: =?utf-8?b?w4FsaWNl?= <[email protected]>' in text_type(mail)
     mail.cc = (u'ćć', '*****@*****.**')
     assert 'Cc: =?utf-8?b?xIfEhw==?= <*****@*****.**>' in text_type(mail)
     mail.reply_to = (u'nóréply', '*****@*****.**')
     assert 'Reply-To: =?utf-8?b?bsOzcsOpcGx5?= <*****@*****.**>' in text_type(mail)
     mail.to = ['á <*****@*****.**>', u'ä <*****@*****.**>']
     assert 'To: =?utf-8?b?w6E=?= <*****@*****.**>, =?utf-8?b?w6Q=?= <*****@*****.**>' in text_type(mail)
Exemplo n.º 7
0
 def test_mail_init(self, mail):
     assert text_type(mail.subject) == 'Down the Rabbit-Hole'
     assert mail.text == 'What is the use of a book without pictures or conversation?'
     assert mail.from_addr == 'Alice from Wonderland <*****@*****.**>'
     assert mail.to == ['*****@*****.**', '*****@*****.**']
     assert mail.cc == ['*****@*****.**']
     assert mail.bcc == ['*****@*****.**']
     assert mail.reply_to == '*****@*****.**'
Exemplo n.º 8
0
def test_smtp_missed_password(recwarn):
    smtp = SMTPMailer(password='******')
    w = recwarn.pop()
    assert text_type(w.message) == (
        'Invalid credentials. Please setup both username and '
        'password or neither.'
    )
    assert smtp.password is None
Exemplo n.º 9
0
 def test_strip_newlines_from_address(self):
     addr = Address(('Alice\n', 'alice\r\[email protected]\r'))
     assert text_type(addr) == 'Alice <*****@*****.**>'
Exemplo n.º 10
0
 def test_encode_nonascii_strings(self):
     addr = Address((u'Álice', u'*****@*****.**'))
     assert text_type(addr) == '=?utf-8?b?w4FsaWNl?= <*****@*****.**>'
Exemplo n.º 11
0
 def test_unpack_address_from_tuple(self):
     addr = Address(('Alice', '*****@*****.**'))
     assert text_type(addr) == u'Alice <*****@*****.**>'
Exemplo n.º 12
0
 def test_unpack_address_from_list(self):
     addr = Address(['Alice', '*****@*****.**'])
     assert text_type(addr) == 'Alice <*****@*****.**>'
Exemplo n.º 13
0
 def test_address_init(self, alice):
     assert alice.address == '*****@*****.**'
     assert text_type(alice) == '*****@*****.**'
Exemplo n.º 14
0
 def test_mail_to_string(self, mail):
     assert text_type(mail) == '''\
Exemplo n.º 15
0
 def test_assign_address_instance(self, alice):
     self.addresses = alice
     assert self.addresses == [alice,]
     assert text_type(self.addresses) == text_type(alice)
Exemplo n.º 16
0
Arquivo: mail.py Projeto: TeZzo1/MQTT
 def __str__(self):
     if isinstance(self.value, string_types):
         return rfc_compliant(''.join(self.value.splitlines()), self.encoding)
     return text_type(self.value)
Exemplo n.º 17
0
 def test_string_representation(self):
     assert text_type(self.subject) == ''
     self.subject = 'Hello'
     assert text_type(self.subject) == 'Hello'
Exemplo n.º 18
0
Arquivo: mail.py Projeto: TeZzo1/MQTT
 def __len__(self):
     return len(text_type(self))
Exemplo n.º 19
0
 def test_append_named_address_to_list(self):
     self.addresses.append(['Alice', '*****@*****.**'])
     assert text_type(self.addresses) == 'Alice <*****@*****.**>'
Exemplo n.º 20
0
 def test_append_address_instance_to_list(self, alice):
     self.addresses.append(alice)
     assert text_type(self.addresses) == '*****@*****.**'
Exemplo n.º 21
0
 def test_append_value_to_list(self):
     self.addresses.append('*****@*****.**')
     assert text_type(self.addresses) == '*****@*****.**'
Exemplo n.º 22
0
 def test_assign_single_address(self):
     address = '*****@*****.**'
     self.addresses = address
     assert self.addresses == [address,]
     assert text_type(self.addresses) == address
Exemplo n.º 23
0
 def __str__(self):
     if isinstance(self.value, string_types):
         return rfc_compliant(''.join(self.value.splitlines()),
                              self.encoding)
     return text_type(self.value)
Exemplo n.º 24
0
 def test_assign_list_of_addresses(self):
     addresses = ['*****@*****.**', '*****@*****.**']
     self.addresses = addresses
     assert self.addresses == addresses
     assert text_type(self.addresses) == ', '.join(addresses)
Exemplo n.º 25
0
 def test_assign_list_of_named_addresses(self):
     named_address = ('Alice', '*****@*****.**')
     self.addresses = [named_address,]
     assert self.addresses == [named_address,]
     assert text_type(self.addresses) == 'Alice <*****@*****.**>'
Exemplo n.º 26
0
 def test_use_encoding_to_encode_nonascii_characters(self):
     self.subject = u'Привет'
     assert text_type(self.subject) == '=?utf-8?b?0J/RgNC40LLQtdGC?='
     self.subject.encoding = 'cp1251'
     assert text_type(self.subject) == '=?cp1251?b?z/Do4uXy?='
Exemplo n.º 27
0
 def __len__(self):
     return len(text_type(self))
Exemplo n.º 28
0
 def test_prevent_header_injection(self):
     self.subject = 'Hello\r\n'
     assert text_type(self.subject) == 'Hello'
Exemplo n.º 29
0
 def send(self, message):
     """Send the message."""
     with self as con:
         message.from_addr = message.from_addr or self.default_sender
         con.sendmail(text_type(message.from_addr),
                      text_type(message.send_to), message.format())