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
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())
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)))
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)) )
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)
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 == '*****@*****.**'
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
def test_strip_newlines_from_address(self): addr = Address(('Alice\n', 'alice\r\[email protected]\r')) assert text_type(addr) == 'Alice <*****@*****.**>'
def test_encode_nonascii_strings(self): addr = Address((u'Álice', u'*****@*****.**')) assert text_type(addr) == '=?utf-8?b?w4FsaWNl?= <*****@*****.**>'
def test_unpack_address_from_tuple(self): addr = Address(('Alice', '*****@*****.**')) assert text_type(addr) == u'Alice <*****@*****.**>'
def test_unpack_address_from_list(self): addr = Address(['Alice', '*****@*****.**']) assert text_type(addr) == 'Alice <*****@*****.**>'
def test_address_init(self, alice): assert alice.address == '*****@*****.**' assert text_type(alice) == '*****@*****.**'
def test_mail_to_string(self, mail): assert text_type(mail) == '''\
def test_assign_address_instance(self, alice): self.addresses = alice assert self.addresses == [alice,] assert text_type(self.addresses) == text_type(alice)
def __str__(self): if isinstance(self.value, string_types): return rfc_compliant(''.join(self.value.splitlines()), self.encoding) return text_type(self.value)
def test_string_representation(self): assert text_type(self.subject) == '' self.subject = 'Hello' assert text_type(self.subject) == 'Hello'
def __len__(self): return len(text_type(self))
def test_append_named_address_to_list(self): self.addresses.append(['Alice', '*****@*****.**']) assert text_type(self.addresses) == 'Alice <*****@*****.**>'
def test_append_address_instance_to_list(self, alice): self.addresses.append(alice) assert text_type(self.addresses) == '*****@*****.**'
def test_append_value_to_list(self): self.addresses.append('*****@*****.**') assert text_type(self.addresses) == '*****@*****.**'
def test_assign_single_address(self): address = '*****@*****.**' self.addresses = address assert self.addresses == [address,] assert text_type(self.addresses) == address
def test_assign_list_of_addresses(self): addresses = ['*****@*****.**', '*****@*****.**'] self.addresses = addresses assert self.addresses == addresses assert text_type(self.addresses) == ', '.join(addresses)
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 <*****@*****.**>'
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?='
def test_prevent_header_injection(self): self.subject = 'Hello\r\n' assert text_type(self.subject) == 'Hello'