def test_dkim_sign_twice(): # Test #44: # " if you put the open there and send more than one messages it fails # (the first works but the next will not if you dont seek(0) the dkim file first)" # Actually not. priv_key, pub_key = _generate_key(length=1024) message = Message(**common_email_data()) message.dkim(key=NativeStringIO(to_native(priv_key)), selector='_dkim', domain='somewhere.net') for n in range(2): message.subject = 'Test %s' % n assert _check_dkim(message, pub_key)
def test_message_addresses(): m = Message() m.mail_from = "웃 <[email protected]>" assert m.mail_from == ("웃", "[email protected]") m.mail_from = ["웃", "[email protected]"] assert m.mail_from == ("웃", "[email protected]") m.mail_to = ("웃", "[email protected]") assert m.mail_to == [("웃", "[email protected]"), ] m.mail_to = [("웃", "[email protected]"), "[email protected]"] assert m.mail_to == [("웃", "[email protected]"), (None, "[email protected]")]
def send(to, subject, body, html=None, copy=None, attachments=None): if not attachments: attachments = [] message = Message(text=body, html=html, subject=subject, mail_from=config.FROM_EMAIL) for filename, attachment, mime in attachments: message.attach(filename=filename, data=attachment, mime=f"{mime} charset=utf-8") if not config.SEND_EMAILS: body = body.replace("https", "http") return print("Sending email", str(body.encode('utf-8'))) message.send( to=to, smtp={ "host": config.SMTP_HOST, "user": config.SMTP_LOGIN, "password": config.SMTP_PASSWORD, "port": "465", "ssl": True, }, )
def test_dkim(): priv_key, pub_key = _generate_key(length=1024) DKIM_PARAMS = [ dict(key=NativeStringIO(to_native(priv_key)), selector='_dkim', domain='somewhere1.net'), dict(key=priv_key, selector='_dkim', domain='somewhere2.net'), # legacy key argument name dict(privkey=priv_key, selector='_dkim', domain='somewhere3.net'), ] if is_py26: load_email_charsets() for dkimparams in DKIM_PARAMS: message = Message(**common_email_data()) message.dkim(**dkimparams) # check DKIM header exist assert message.as_message()['DKIM-Signature'] #print(__name__, "type message.as_string()==", type(message.as_string())) #print(message.as_string()) #print(type(message.as_string())) #print(email.__file__) #print(email.charset.CHARSETS) #print('adding utf-8 charset...') #email.charset.add_charset('utf-8', email.charset.BASE64, email.charset.BASE64) #print(email.charset.CHARSETS) assert 'DKIM-Signature: ' in message.as_string() assert _check_dkim(message, pub_key)
def _send_message(data: List[Dict], message: Message) -> bool: now = datetime.utcnow() return message.send( to=[str(e) for e in params.email.recipients], render={ "states": data, "timestamp": now.strftime("%A, %B %-d, %Y (%I:%M:%S UTC)"), }, smtp={ "host": params.email.smtp_host, "port": params.email.smtp_port }, )
def test_headers_not_double_encoded(): TEXT = '웃' m = Message() m.mail_from = (TEXT, '[email protected]') m.mail_to = (TEXT, '[email protected]') m.subject = TEXT m.html = '...' msg = m.as_message() assert decode_header(parseaddr(msg['From'])[0]) == TEXT assert decode_header(parseaddr(msg['To'])[0]) == TEXT assert decode_header(msg['Subject']) == TEXT
def test_several_recipients(): # Test multiple recipients in "To" header params = dict(html='...', mail_from='[email protected]') m = Message(mail_to=['[email protected]', '[email protected]'], cc='[email protected]', **params) assert m.as_message()['To'] == '[email protected], [email protected]' assert m.as_message()['cc'] == '[email protected]' m = Message(mail_to=[('♡', '[email protected]'), ('웃', '[email protected]')], **params) assert m.as_message( )['To'] == '=?utf-8?b?4pmh?= <[email protected]>, =?utf-8?b?7JuD?= <[email protected]>' # Test sending to several emails backend = InMemoryBackend() m = Message(mail_to=[('♡', '[email protected]'), ('웃', '[email protected]')], cc=['[email protected]', '[email protected]'], bcc=['[email protected]', '[email protected]'], **params) m.send(smtp=backend) for addr in ['[email protected]', '[email protected]', '[email protected]', '[email protected]']: assert len(backend.messages[addr]) == 1
def test_headers_ascii_encoded(): """ Test we encode To/From header only when it not-ascii """ for text, encoded in ( ('웃', '=?utf-8?b?7JuD?='), ('ascii text', 'ascii text'), ): msg = Message(mail_from=(text, '[email protected]'), mail_to=(text, '[email protected]'), subject=text, html='...').as_message() assert parseaddr(msg['From'])[0] == encoded assert parseaddr(msg['To'])[0] == encoded
def test_dkim(): priv_key, pub_key = _generate_key(length=1024) DKIM_PARAMS = [dict(key=NativeStringIO(to_native(priv_key)), selector='_dkim', domain='somewhere1.net'), dict(key=priv_key, selector='_dkim', domain='somewhere2.net'), # legacy key argument name dict(privkey=priv_key, selector='_dkim', domain='somewhere3.net'), ] if is_py26: load_email_charsets() for dkimparams in DKIM_PARAMS: message = Message(**common_email_data()) message.dkim(**dkimparams) # check DKIM header exist assert message.as_message()['DKIM-Signature'] #print(__name__, "type message.as_string()==", type(message.as_string())) #print(message.as_string()) #print(type(message.as_string())) #print(email.__file__) #print(email.charset.CHARSETS) #print('adding utf-8 charset...') #email.charset.add_charset('utf-8', email.charset.BASE64, email.charset.BASE64) #print(email.charset.CHARSETS) assert 'DKIM-Signature: ' in message.as_string() assert _check_dkim(message, pub_key)
def test_message_id(): params = dict(html='...', mail_from='[email protected]', mail_to='[email protected]') # Check message-id not exists by default m = Message(**params) assert not m.as_message()['Message-ID'] # Check message-id property setter m.message_id = 'ZZZ' assert m.as_message()['Message-ID'] == 'ZZZ' # Check message-id exists when argument specified m = Message(message_id=MessageID(), **params) assert m.as_message()['Message-ID'] m = Message(message_id='XXX', **params) assert m.as_message()['Message-ID'] == 'XXX'
def test_lazy_translated(): # prepare translations T = gettext.GNUTranslations() T._catalog = {'invitation': 'invitaci\xf3n'} _ = T.gettext msg = Message(html='...', subject=lazy_string(_, 'invitation')) assert decode_header(msg.as_message()['subject']) == _('invitation') msg = Message(html='...', subject='invitaci\xf3n') assert decode_header(msg.as_message()['subject']) == 'invitaci\xf3n'
def test_several_recipients(): # Test multiple recipients in "To" header params = dict(html='...', mail_from='[email protected]') m = Message(mail_to=['[email protected]', '[email protected]'], cc='[email protected]', **params) assert m.as_message()['To'] == '[email protected], [email protected]' assert m.as_message()['cc'] == '[email protected]' m = Message(mail_to=[('♡', '[email protected]'), ('웃', '[email protected]')], **params) assert m.as_message()['To'] == '=?utf-8?b?4pmh?= <[email protected]>, =?utf-8?b?7JuD?= <[email protected]>' # Test sending to several emails backend = InMemoryBackend() m = Message(mail_to=[('♡', '[email protected]'), ('웃', '[email protected]')], cc=['[email protected]', '[email protected]'], bcc=['[email protected]', '[email protected]'], **params) m.send(smtp=backend) for addr in ['[email protected]', '[email protected]', '[email protected]', '[email protected]']: assert len(backend.messages[addr]) == 1
def test_message_addresses(): m = Message() m.mail_from = "웃 <[email protected]>" assert m.mail_from == ("웃", "[email protected]") m.mail_from = ["웃", "[email protected]"] assert m.mail_from == ("웃", "[email protected]") m.mail_to = ("웃", "[email protected]") assert m.mail_to == [ ("웃", "[email protected]"), ] m.mail_to = [("웃", "[email protected]"), "[email protected]"] assert m.mail_to == [("웃", "[email protected]"), (None, "[email protected]")]
def test_transform(): message = Message( html='''<style>h1{color:red}</style><h1>Hello world!</h1>''') message.transform() assert message.html == '<html><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"/></head>' \ '<body><h1 style="color:red">Hello world!</h1></body></html>'
def test_rfc6532_address(): m = Message() m.mail_to = "anaï[email protected]" m.html = 'X' assert m.as_string()
def test_transform(): message = Message(html='''<style>h1{color:red}</style><h1>Hello world!</h1>''') message.transform() assert message.html == '<html><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"/></head>' \ '<body><h1 style="color:red">Hello world!</h1></body></html>'