Skip to content

Commit 8f8b0db

Browse files
committed
update
1 parent 6c8df41 commit 8f8b0db

1 file changed

Lines changed: 39 additions & 70 deletions

File tree

Mailman/Handlers/SMTPDirect.py

Lines changed: 39 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -157,66 +157,40 @@ def quit(self):
157157

158158

159159
def process(mlist, msg, msgdata):
160-
try:
161-
# Convert email.message.Message to Mailman.Message.Message if needed
162-
if isinstance(msg, email.message.Message):
163-
newmsg = Message()
164-
# Copy attributes
165-
for k, v in msg.items():
166-
newmsg[k] = v
167-
# Copy payload with proper MIME handling
168-
if msg.is_multipart():
169-
for part in msg.get_payload():
170-
if isinstance(part, email.message.Message):
171-
newmsg.attach(part)
172-
else:
173-
# Handle non-Message payloads
174-
newpart = Message()
175-
newpart.set_payload(part)
176-
newmsg.attach(newpart)
177-
else:
178-
newmsg.set_payload(msg.get_payload())
179-
msg = newmsg
180-
181-
recips = msgdata.get('recips')
182-
if not recips:
183-
# Nobody to deliver to!
184-
return
185-
# Calculate the non-VERP envelope sender.
186-
envsender = msgdata.get('envsender')
187-
if envsender is None:
188-
if mlist:
189-
envsender = mlist.GetBouncesEmail()
190-
else:
191-
envsender = Mailman.Utils.get_site_email(extra='bounces')
192-
193-
# Time to split up the recipient list
194-
deliveryfunc = None
195-
if ('personalize' not in msgdata or msgdata['personalize']) and (
196-
msgdata.get('verp') or mlist.personalize):
197-
chunks = [[recip] for recip in recips]
198-
msgdata['personalize'] = 1
199-
deliveryfunc = verpdeliver
200-
elif Mailman.mm_cfg.SMTP_MAX_RCPTS <= 0:
201-
chunks = [recips]
202-
else:
203-
chunks = chunkify(recips, Mailman.mm_cfg.SMTP_MAX_RCPTS)
204-
205-
# See if this is an unshunted message for which some were undelivered
206-
if 'undelivered' in msgdata:
207-
chunks = msgdata['undelivered']
160+
"""Process the message for delivery.
208161
209-
# If we're doing bulk delivery, then we can stitch up the message now.
210-
if deliveryfunc is None:
211-
# Be sure never to decorate the message more than once!
212-
if not msgdata.get('decorated'):
213-
decorate(mlist, msg, msgdata)
214-
msgdata['decorated'] = True
215-
deliveryfunc = bulkdeliver
162+
This is the main entry point for the SMTPDirect handler.
163+
"""
164+
t0 = time.time()
165+
refused = {}
166+
envsender = msgdata.get('envsender', msg.get_sender())
167+
if envsender is None:
168+
envsender = mlist.GetBouncesEmail()
169+
# Get the list of recipients
170+
recips = msgdata.get('recipients', [])
171+
if not recips:
172+
mailman_log('error', 'No recipients found in msgdata')
173+
return
174+
175+
# Check for spam headers first
176+
if msg.get('x-google-group-id'):
177+
mailman_log('error', 'Rejecting message with X-Google-Group-Id header')
178+
# Add all recipients to refused list with 550 error
179+
for r in recips:
180+
refused[r] = (550, 'Message rejected due to spam detection')
181+
# Update failures dict
182+
msgdata['failures'] = refused
183+
return
184+
185+
# Chunkify the recipients
186+
chunks = chunkify(recips, Mailman.mm_cfg.SMTP_MAX_RCPTS_PER_CHUNK)
187+
# Choose the delivery function based on VERP settings
188+
if msgdata.get('verp'):
189+
deliveryfunc = verpdeliver
190+
else:
191+
deliveryfunc = bulkdeliver
216192

217-
refused = {}
218-
t0 = time.time()
219-
# Open the initial connection
193+
try:
220194
origrecips = msgdata['recips']
221195
origsender = msgdata.get('original_sender', msg.get_sender())
222196
conn = Connection()
@@ -227,6 +201,13 @@ def process(mlist, msg, msgdata):
227201
msgdata['recips'] = chunk
228202
try:
229203
deliveryfunc(mlist, msg, msgdata, envsender, refused, conn)
204+
except Mailman.Errors.RejectMessage as e:
205+
# Handle message rejection gracefully
206+
mailman_log('error', 'Message rejected: %s', str(e))
207+
# Add all recipients in this chunk to refused list
208+
for r in chunk:
209+
refused[r] = (550, str(e))
210+
continue
230211
except Exception as e:
231212
mailman_log('error',
232213
'Delivery error for chunk: %s\nError: %s\n%s',
@@ -392,18 +373,6 @@ def bulkdeliver(mlist, msg, msgdata, envsender, failures, conn):
392373
recips = []
393374
refused = {}
394375
try:
395-
# Check for spam headers first
396-
if msg.get('x-google-group-id'):
397-
mailman_log('error', 'Rejecting message with X-Google-Group-Id header')
398-
# Add all recipients to refused list with 550 error
399-
for r in msgdata.get('recipients', []):
400-
refused[r] = (550, 'Message rejected due to spam detection')
401-
# Update failures dict
402-
failures.update(refused)
403-
msgdata['failures'] = failures
404-
# Raise RejectMessage to properly handle the rejection
405-
raise Mailman.Errors.RejectMessage('Message rejected due to spam detection')
406-
407376
# Get the list of recipients
408377
recips = msgdata.get('recipients', [])
409378
if not recips:

0 commit comments

Comments
 (0)