 cfdbcc8ee1
			
		
	
	
		cfdbcc8ee1
		
	
	
	
	
		
			
			Není úplně žádoucí, aby se v mailu z testwebu poslala spousta adres, protože je to náchylné na automatické zpracování v případě, že se k takovému mailu dostane někdo další.
		
			
				
	
	
		
			35 lines
		
	
	
	
		
			1.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			35 lines
		
	
	
	
		
			1.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| """A simple email backend, which only prepends all subjects with a string.
 | |
| 
 | |
| Used to distinguish testing emails from production ones."""
 | |
| 
 | |
| # We try to monkeypatch django.core.mail.backends.smtp :-)
 | |
| 
 | |
| from django.core.mail.backends.smtp import EmailBackend as DjangoSMTPBackend
 | |
| from django.conf import settings
 | |
| 
 | |
| def omezovatko_poctu_mailu(maily:list, maximum:int) -> str:
 | |
| 	if len(maily) <= maximum: return str(maily)
 | |
| 	# Aspoň zhruba simulujeme tisk pole…
 | |
| 	return '[' + ", ".join(f"'{mail}'" for mail in maily[:maximum - 1]) + f', … ({len(maily)} e-mailů) ]'
 | |
| 
 | |
| class PrefixingMailBackend(DjangoSMTPBackend):
 | |
| 	# method _send is not probably meant to be monkey_patched, so we patch send_messages instead.
 | |
| 	def send_messages(self, messages):
 | |
| 		prefix = '[Mail z testwebu]'
 | |
| 		for message in messages:
 | |
| 			# We hope that this is a django.core.mail.message.EmailMessage
 | |
| 
 | |
| 			if message.from_email != settings.SERVER_EMAIL:
 | |
| 				message.subject = prefix + ' ' + message.subject
 | |
| 				to = omezovatko_poctu_mailu(message.to, 3)
 | |
| 				cc = omezovatko_poctu_mailu(message.cc, 3)
 | |
| 				bcc = omezovatko_poctu_mailu(message.bcc, 3)
 | |
| 				message.body = f"""Bylo by posláno na e-maily:
 | |
| 	To: {to}
 | |
| 	Cc: {cc}
 | |
| 	Bcc: {bcc}
 | |
| """+ "\n\n" + message.body
 | |
| 				message.to = settings.TESTOVACI_EMAILOVA_KONFERENCE
 | |
| 				message.cc = []
 | |
| 				message.bcc = []
 | |
| 		return super().send_messages(messages)
 |