def test_get_app_url_bad(self): with patch('dns.resolver.query', self.resolver): org = OrganizationFactory() domain = SendingDomainFactory( app_domain=bad_app_domain, organization=org) url = get_app_url(domain=domain, organization=org) self.assertEqual(url, settings.APPLICATION_URL)
def unsubscribe_url(cls, identifier, author, sending_domain): from munch.core.mail.utils import get_app_url relative_url = reverse('unsubscribe', kwargs={'identifier': identifier}) organization_app_url = get_app_url(organization=author.organization, domain=sending_domain) return urllib.parse.urljoin(organization_app_url, relative_url)
def test_get_app_url_good(self): with patch('dns.resolver.query', self.resolver): org = OrganizationFactory() domain = SendingDomainFactory( app_domain=ok_app_domain, organization=org) # retrieve object from DB to get latest app_domain_status domain = SendingDomain.objects.get(pk=domain.pk) url = get_app_url(domain=domain, organization=org) def_u = urlparse(settings.APPLICATION_URL.strip('/')) app_u = '{}://{}'.format(def_u.scheme, ok_app_domain) if def_u.port: app_u += ':{}'.format(def_u.port) self.assertEqual(url, app_u)
def apply(self, item, policies_list): subscription_url = get_app_url(organization=item.get_owner()) if 'BounceCheck' in policies_list: return_path = 'subscription-bounce+{uuid}@{fqdn}'.format( uuid=item.uuid, fqdn=settings.RETURNPATH_DOMAIN) else: return_path = settings.SERVICE_MSG_FROM_EMAIL confirmation_link = urljoin( subscription_url, reverse('confirmation', kwargs={'uuid': item.uuid})) message = EmailMessage( subject='Votre inscription', body=render_to_string( 'contacts/double_opt_in_confirmation.txt', {'contact': item, 'confirmation_link': confirmation_link}), to=(item.address, ), from_email='{} <{}>'.format( settings.SERVICE_MSG_FROM_NAME, settings.SERVICE_MSG_FROM_EMAIL), headers={ 'Auto-Submitted': 'auto-generated', 'Return-Path': return_path}) message.send()
def get_app_url(self): from munch.core.mail.utils import get_app_url return get_app_url(domain=self.get_sending_domain(), organization=self.get_organization())
def apply(self, envelope): identifier = envelope.headers.get( settings.TRANSACTIONAL['X_MESSAGE_ID_HEADER']) if not identifier or not envelope.client.get('auth'): # FIXME: log exact error raise PermanentRelayError('Internal application error') # Process mail content munchers_kwargs = { 'mail_identifier': identifier, 'app_url': get_app_url(domain=envelope.sending_domain, organization=envelope.organization), } msg_links = {} mail_kwargs = {} track_open = envelope.headers.get( settings.TRANSACTIONAL.get('X_MAIL_TRACK_OPEN_HEADER', None)) track_open_done = False if track_open: munchers_kwargs.update({'track_open': True}) mail_kwargs.update({'track_open': True}) track_clicks = envelope.headers.get( settings.TRANSACTIONAL.get('X_MAIL_TRACK_CLICKS_HEADER', None)) if track_clicks: mail_kwargs.update({'track_clicks': True}) munchers_kwargs.update({'track_clicks': True}) add_unsubscribe = envelope.headers.get( settings.TRANSACTIONAL.get('X_MAIL_UNSUBSCRIBE_HEADER', None)) if add_unsubscribe: munchers_kwargs.update({ 'unsubscribe_url': Mail.unsubscribe_url(identifier, envelope.user, envelope.sending_domain) }) # Retrieve full html to extract links html = '' message = email.message_from_bytes(b'\n'.join(envelope.flatten())) for part in message.walk(): if part.get_content_type() == 'text/html': html += part.get_payload() msg_links = get_msg_links(html) mail_kwargs.update({'msg_links': msg_links}) munchers_kwargs.update({'links_map': msg_links}) # Walk throught every parts to apply munchers on it for part in message.walk(): if part.get_content_type() == 'text/html': html = part.get_payload() if track_open and not track_open_done: html = add_tracking_image(html, **munchers_kwargs) track_open_done = True if track_clicks: html = rewrite_html_links(html, **munchers_kwargs) part.set_payload(html) content = part.get_payload() if add_unsubscribe: content = set_unsubscribe_url(content, **munchers_kwargs) part.set_payload(content) envelope.parse_msg(message) batch = envelope.headers.get( settings.TRANSACTIONAL.get('X_MAIL_BATCH_HEADER', None)) if batch: batch, created = MailBatch.objects.get_or_create( name=batch, author=envelope.user, defaults={'msg_links': msg_links}) if not created: batch.msg_links = msg_links batch.save() category = envelope.headers.get( settings.TRANSACTIONAL.get('X_MAIL_BATCH_CATEGORY_HEADER', None)) if category: category, _ = Category.objects.get_or_create( author=envelope.user, name=category) batch.category = category batch.save() raw_mail, _ = RawMail.objects.get_or_create(content=envelope.message) mail = Mail.objects.create( author=envelope.user, batch=batch, identifier=envelope.headers.get( settings.TRANSACTIONAL['X_MESSAGE_ID_HEADER']), headers={ k: v.encode('utf-8', 'surrogateescape').decode('utf-8') for k, v in envelope.headers.raw_items() }, message=raw_mail, sender=envelope.sender, recipient=envelope.recipients[0], **mail_kwargs) MailStatus.objects.create(mail=mail, destination_domain=extract_domain( envelope.recipients[0]))
def test_get_app_url_default(self): org = OrganizationFactory() domain = SendingDomainFactory(organization=org) url = get_app_url(domain=domain, organization=org) self.assertEqual(url, settings.APPLICATION_URL)