def test_handle_non_unicode_char(self): """ Test handling a non-unicode character in subject or body """ s = '€' msg = EmailMultiAlternatives(subject=s, body=s, to=['*****@*****.**']) _send_celery_mail(msg)
def send_contact_email(message, contact_email): subject = _('Contact message from %(sender)s') % {'sender': message.name} from_email = properties.CONTACT_EMAIL if not from_email: return # add the url to the backoffice lang_link = reverse('admin:contact_contactmessage_change', args=[message.id]) # Strip language from reverse generated link m = re.match(r'(/[^/]*)(/.*$)', lang_link) try: link = m.groups()[1] except IndexError: link = lang_link ctx = ClientContext( {'message': message, 'link': link, 'site_url': tenant_url(), 'site': tenant_site()}) text_content = render_to_string('contact/contact.mail.txt', context_instance=ctx) html_content = render_to_string('contact/contact.mail.html', context_instance=ctx) msg = EmailMultiAlternatives(subject=subject, body=text_content, to=[contact_email], from_email=from_email) msg.attach_alternative(html_content, "text/html") msg.send()
def test_tenant_dkim_settings(self, smtp): """ test setup where tenant config differs from global settings """ with mock.patch("bluebottle.utils.email_backend.properties", new=mock.Mock([])) as properties: properties.MAIL_CONFIG = {'HOST': 'tenanthost', 'PORT': 4242} properties.DKIM_SELECTOR = "key2" properties.DKIM_DOMAIN = "testserver" properties.DKIM_PRIVATE_KEY = DKIM_PRIVATE_KEY be = TenantAwareBackend() msg = EmailMultiAlternatives(subject="test", body="test", to=["*****@*****.**"]) be.open() connection = be.connection be.send_messages([msg]) to_bytes = lambda s: force_bytes(s, 'utf-8') def _plain_key(s): return b"".join( [l for l in s.split(b'\n') if not l.startswith(b'---')]) signed_msg = connection.sendmail.call_args[0][2] dkim_message = dkim.DKIM(message=to_bytes(signed_msg)) dkim_check = dkim_message.verify(dnsfunc=lambda name: b"".join( [b"v=DKIM1; p=", _plain_key(DKIM_PUBLIC_KEY)])) self.assertTrue(signed_msg.find("d=testserver") >= 0) self.assertTrue(signed_msg.find("s=key2") >= 0) self.assertTrue(dkim_check, "Email should be signed by tenant")
def test_no_unicode_encode_error(self): """ Test handling a unicode character in subject or body """ s = u'\u2019' msg = EmailMultiAlternatives(subject=s, body=s, to=['*****@*****.**']) try: _send_celery_mail(msg) except UnicodeEncodeError: self.fail("Unicode string not handled correctly")
def test_reply_to(self): """ Test simple / traditional case where config comes from settings """ with mock.patch("bluebottle.clients.mail.properties", new=mock.Mock([])) as properties: reply_to = '*****@*****.**' properties.TENANT_MAIL_PROPERTIES = { 'address': '*****@*****.**', 'sender': 'Info Tester', 'reply_to': reply_to } msg = EmailMultiAlternatives(subject="test", body="test", to=["*****@*****.**"]) self.assertEquals(msg.extra_headers['Reply-To'], reply_to) self.assertEquals(msg.from_email, 'Info Tester <*****@*****.**>')
def create_message(template_name=None, to=None, subject=None, cc=None, bcc=None, from_email=None, reply_to=None, attachments=None, **kwargs): if hasattr(to, 'primary_language') and to.primary_language: language = to.primary_language else: language = properties.LANGUAGE_CODE # This is an exception to handle a Bookingcares.com language which # contains more languages than the rest of the platform if 'language' in kwargs: language = kwargs['language'] with TenantLanguage(language): ctx = ClientContext(kwargs) ctx['to'] = to # Add the recipient to the context text_content = get_template('{0}.txt'.format(template_name)).render( ctx.flatten()) html_content = get_template('{0}.html'.format(template_name)).render( ctx.flatten()) args = dict(subject=subject, body=text_content, to=[to.email]) if cc: args['cc'] = cc if bcc: args['bcc'] = bcc if reply_to: args['reply_to'] = [reply_to] # even if it's None args['from_email'] = from_email args['attachments'] = attachments # Calling force_unicode on the subject below in case the subject # is being translated using ugettext_lazy. msg = EmailMultiAlternatives(**args) msg.activated_language = translation.get_language() msg.attach_alternative(html_content, "text/html") return msg
def test_settings_config(self, smtp): """ Test simple / traditional case where config comes from settings """ be = TenantAwareBackend() msg = EmailMultiAlternatives(subject="test", body="test", to=["*****@*****.**"]) # open the connection explicitly so we can get the # connection reference. It will be cleared once closed # in send_messages be.open() connection = be.connection be.send_messages([msg]) self.assertTrue(smtp.called) self.assertEquals(smtp.call_args[0], ('somehost', 1337)) self.assertTrue(connection.sendmail.called)
def test_tenant_config(self, smtp): """ test setup where tenant config differs from global settings """ with mock.patch("bluebottle.utils.email_backend.properties", new=mock.Mock([])) as properties: properties.MAIL_CONFIG = {'HOST': 'tenanthost', 'PORT': 4242} be = TenantAwareBackend() msg = EmailMultiAlternatives(subject="test", body="test", to=["*****@*****.**"]) # open the connection explicitly so we can get the # connection reference. It will be cleared once closed # in send_messages be.open() connection = be.connection be.send_messages([msg]) self.assertTrue(smtp.called) self.assertEquals(smtp.call_args[0], ('tenanthost', 4242)) self.assertTrue(connection.sendmail.called)