Fix: add SMTP helpers and fallback to domain:587 #7

Merged
jason merged 1 commit from jch/fix-smtp-fallback-localhost into main 2026-07-10 10:32:38 -04:00
Owner

What's changed

This commit adds the missing _has_starttls() and _has_auth() helper functions that were referenced but never defined (causing NameError when merged). It also implements a robust SMTP fallback mechanism for the send_email method.

Fix 1: Undefined helpers _has_starttls() and _has_auth()

  • These functions parse the raw EHLO response bytes to detect server capabilities
  • Without these, the code failed with NameError: name '_has_starttls' is not defined when attempting to send emails

Fix 2: Fallback SMTP to domain:587 with STARTTLS + Auth

When the primary SMTP connection fails (e.g., localhost:25 rejecting unauthenticated sender), the bridge now tries a fallback:

  1. Connects to <domain>:587
  2. Performs EHLO then STARTTLS if advertised
  3. Authenticates using bridge user credentials from users.conf.json (credentials['jid'] and credentials['password'])
  4. Sends the email

This fixes the issue where Postfix on localhost:25 rejects email senders not locally logged in, but allows the bridge to authenticate with STARTTLS+AUTH on port 587.

Technical details:

  • Primary SMTP exception handling catches SMTPException (not broad Exception)
  • Fallback only runs when primary fails (if not primary_failed: return)
  • Variable scoping fixed so both primary and fallback errors are visible in error message
  • Socket-level errors handled separately so connection failures show appropriate messages
  • fb = None initialized before try block prevents NameError in finally clause

How to test

  1. Deploy to a host where Postfix rejects unauthenticated senders on localhost:25
  2. Send an XMPP message that triggers email forwarding
  3. Check logs for transition from primary failure to fallback success:
    Primary SMTP (localhost:25) failed: 553 ... trying fallback hammons.llc:587
    Email sent via fallback hammons.llc:587
    
## What's changed This commit adds the missing `_has_starttls()` and `_has_auth()` helper functions that were referenced but never defined (causing NameError when merged). It also implements a robust SMTP fallback mechanism for the `send_email` method. ### Fix 1: Undefined helpers `_has_starttls()` and `_has_auth()` - These functions parse the raw EHLO response bytes to detect server capabilities - Without these, the code failed with `NameError: name '_has_starttls' is not defined` when attempting to send emails ### Fix 2: Fallback SMTP to domain:587 with STARTTLS + Auth When the primary SMTP connection fails (e.g., localhost:25 rejecting unauthenticated sender), the bridge now tries a fallback: 1. Connects to `<domain>:587` 2. Performs EHLO then STARTTLS if advertised 3. Authenticates using bridge user credentials from `users.conf.json` (`credentials['jid']` and `credentials['password']`) 4. Sends the email This fixes the issue where Postfix on localhost:25 rejects email senders not locally logged in, but allows the bridge to authenticate with STARTTLS+AUTH on port 587. ### Technical details: - Primary SMTP exception handling catches `SMTPException` (not broad Exception) - Fallback only runs when primary fails (`if not primary_failed: return`) - Variable scoping fixed so both primary and fallback errors are visible in error message - Socket-level errors handled separately so connection failures show appropriate messages - `fb = None` initialized before try block prevents NameError in finally clause ## How to test 1. Deploy to a host where Postfix rejects unauthenticated senders on localhost:25 2. Send an XMPP message that triggers email forwarding 3. Check logs for transition from primary failure to fallback success: ``` Primary SMTP (localhost:25) failed: 553 ... trying fallback hammons.llc:587 Email sent via fallback hammons.llc:587
jason merged commit 551d48067e into main 2026-07-10 10:32:38 -04:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
jason/xmpp-email-bridge!7
No description provided.