3434from email .utils import formataddr , make_msgid
3535import re
3636import smtplib
37+ from typing import Optional
3738
3839from ...common import NotifyFormat , NotifyType
3940from ...conversion import convert_between
@@ -229,7 +230,7 @@ def __init__(
229230 super ().__init__ (** kwargs )
230231
231232 # Acquire Email 'To'
232- self .targets = list ()
233+ self .targets = []
233234
234235 # Acquire Carbon Copies
235236 self .cc = set ()
@@ -457,14 +458,13 @@ def apply_email_defaults(self, secure_mode=None, port=None, **kwargs):
457458
458459 for i in range (len (templates .EMAIL_TEMPLATES )): # pragma: no branch
459460 self .logger .trace (
460- "Scanning %s against %s"
461- % (from_addr , templates .EMAIL_TEMPLATES [i ][0 ])
462- )
461+ f"Scanning { from_addr } "
462+ f"against { templates .EMAIL_TEMPLATES [i ][0 ]} " )
463463 match = templates .EMAIL_TEMPLATES [i ][1 ].match (from_addr )
464464 if match :
465465 self .logger .info (
466- "Applying %s Defaults" % templates .EMAIL_TEMPLATES [i ][0 ],
467- )
466+ f "Applying { templates .EMAIL_TEMPLATES [i ][0 ]} Defaults" )
467+
468468 # the secure flag can not be altered if defined in the template
469469 self .secure = templates .EMAIL_TEMPLATES [i ][2 ].get (
470470 "secure" , self .secure
@@ -692,7 +692,7 @@ def url(self, privacy=False, *args, **kwargs):
692692 # Handle our Carbon Copy Addresses
693693 params ["cc" ] = "," .join ([
694694 formataddr (
695- (self .names [ e ] if e in self . names else False , e ),
695+ (self .names . get ( e , False ) , e ),
696696 # Swap comma for it's escaped url code (if detected) since
697697 # we're using that as a delimiter
698698 charset = "utf-8" ,
@@ -704,7 +704,7 @@ def url(self, privacy=False, *args, **kwargs):
704704 # Handle our Blind Carbon Copy Addresses
705705 params ["bcc" ] = "," .join ([
706706 formataddr (
707- (self .names [ e ] if e in self . names else False , e ),
707+ (self .names . get ( e , False ) , e ),
708708 # Swap comma for it's escaped url code (if detected) since
709709 # we're using that as a delimiter
710710 charset = "utf-8" ,
@@ -716,8 +716,8 @@ def url(self, privacy=False, *args, **kwargs):
716716 # Handle our Reply-To Addresses
717717 params ["reply" ] = "," .join ([
718718 formataddr (
719- (self .names [ e ] if e in self . names else False , e ),
720- # Swap comma for it's escaped url code (if detected) since
719+ (self .names . get ( e , False ) , e ),
720+ # Swap comma for its escaped url code (if detected) since
721721 # we're using that as a delimiter
722722 charset = "utf-8" ,
723723 ).replace ("," , "%2C" )
@@ -927,16 +927,16 @@ def prepare_emails(
927927 body ,
928928 from_addr ,
929929 to ,
930- cc = set () ,
931- bcc = set () ,
932- reply_to = set () ,
930+ cc : Optional [ set ] = None ,
931+ bcc : Optional [ set ] = None ,
932+ reply_to : Optional [ set ] = None ,
933933 # Providing an SMTP Host helps improve Email Message-ID
934934 # and avoids getting flagged as spam
935935 smtp_host = None ,
936936 # Can be either 'html' or 'text'
937937 notify_format = NotifyFormat .HTML ,
938938 attach = None ,
939- headers = dict () ,
939+ headers : Optional [ dict ] = None ,
940940 # Names can be a dictionary
941941 names = None ,
942942 # Pretty Good Privacy Support; Pass in an
@@ -970,17 +970,28 @@ def prepare_emails(
970970
971971 Pass in an ApprisePGPController() if you wish to use this
972972 """
973-
974973 if not to :
975974 # There is no one to email; we're done
976975 msg = "There are no Email recipients to notify"
977976 logger .warning (msg )
978- raise AppriseEmailException (msg )
977+ raise AppriseEmailException (msg ) from None
979978
980979 elif pgp and not _pgp .PGP_SUPPORT :
981980 msg = "PGP Support unavailable; install PGPy library"
982981 logger .warning (msg )
983- raise AppriseEmailException (msg )
982+ raise AppriseEmailException (msg ) from None
983+
984+ if headers is None :
985+ headers = {}
986+
987+ if cc is None :
988+ cc = set ()
989+
990+ if bcc is None :
991+ bcc = set ()
992+
993+ if reply_to is None :
994+ reply_to = set ()
984995
985996 if not names :
986997 # Prepare a empty dictionary to prevent errors/warnings
@@ -999,13 +1010,13 @@ def prepare_emails(
9991010 to_name , to_addr = emails .pop (0 )
10001011
10011012 # Strip target out of cc list if in To or Bcc
1002- _cc = cc - bcc - set ([ to_addr ])
1013+ _cc = cc - bcc - { to_addr }
10031014
10041015 # Strip target out of bcc list if in To
1005- _bcc = bcc - set ([ to_addr ])
1016+ _bcc = bcc - { to_addr }
10061017
10071018 # Strip target out of reply_to list if in To
1008- _reply_to = reply_to - set ([ to_addr ])
1019+ _reply_to = reply_to - { to_addr }
10091020
10101021 # Format our cc addresses to support the Name field
10111022 _cc = [
@@ -1124,8 +1135,8 @@ def prepare_emails(
11241135 # Store Autocrypt header (DeltaChat Support)
11251136 base .add_header (
11261137 "Autocrypt" ,
1127- "addr=%s; prefer-encrypt=mutual "
1128- % formataddr (( False , to_addr ), charset = "utf-8" ),
1138+ f "addr={ formataddr (( False , to_addr ), charset = 'utf-8' ) } ; "
1139+ "prefer-encrypt=mutual"
11291140 )
11301141
11311142 # Set Encryption Info Part
@@ -1159,6 +1170,6 @@ def prepare_emails(
11591170
11601171 yield EmailMessage (
11611172 recipient = to_addr ,
1162- to_addrs = [to_addr ] + list (_cc ) + list (_bcc ),
1173+ to_addrs = [to_addr , * list (_cc ), * list (_bcc )] ,
11631174 body = base .as_string (),
11641175 )
0 commit comments