Sample Header Ad - 728x90

Lua SMTP Script with STARTTLS Fails to Authenticate

1 vote
0 answers
44 views
🔍 Context I'm working on a Lua script that sends emails via SMTP with STARTTLS. The script successfully connects to the SMTP server, but authentication sometimes fails with the error: authentication not supported This happens after the STARTTLS handshake. I'm not sure if I'm sending the SMTP commands in the correct order or if there's an issue with the authentication method. ✅ What the Script Does 📡 Connects to an SMTP server (supports both TLS and SSL) 🔒 Implements STARTTLS when using port 587 🔑 Authenticates using AUTH LOGIN 🛠 Includes a debugging function to log SMTP commands and responses ⚠️ Issue Description The script connects to the SMTP server (port 587 or 465). If using port 587, it sends EHLO, then STARTTLS. After TLS is enabled, it tries to authenticate, but the server rejects authentication. If using port 465, the script connects via SSL directly, but authentication still fails. 🛠 Debugging Log Sample Here’s an example of the log output when using port 587:
-bash
[SMTP DEBUG] Sent : EHLO localhost
[SMTP DEBUG] Received : 250-smtp.example.com
[SMTP DEBUG] Sent : STARTTLS
[SMTP DEBUG] Received : 220 Ready to start TLS
[SMTP DEBUG] Sent : EHLO localhost  >SMTP: Connect to SMTP (port 587 or 465)
    alt Port 587 (STARTTLS)
        SMTP->>Client: 220 Service Ready
        Client->>SMTP: EHLO localhost
        SMTP->>Client: 250-AUTH PLAIN LOGIN
        Client->>SMTP: STARTTLS
        SMTP->>Client: 220 Ready to start TLS
        Client->>Client: Upgrade to TLS Mode
        Client->>SMTP: EHLO localhost (again?)
    else Port 465 (SSL Direct)
        Client->>Client: Direct SSL/TLS Connection
    end

    Client->>SMTP: AUTH LOGIN
    SMTP->>Client: 334 Username Prompt
    Client->>SMTP: (Base64 Encoded Email)
    SMTP->>Client: 334 Password Prompt
    Client->>SMTP: (Base64 Encoded Password)
    SMTP->>Client: 235 Authentication Successful

    Client->>SMTP: MAIL FROM:
    SMTP->>Client: 250 OK
    Client->>SMTP: RCPT TO:
    SMTP->>Client: 250 OK
    Client->>SMTP: DATA
    SMTP->>Client: 354 Start mail input
    Client->>SMTP: (Email Content)
    Client->>SMTP: .
    SMTP->>Client: 250 Message Accepted

    Client->>SMTP: QUIT
    SMTP->>Client: 221 Bye
Questions -- Do I need to send EHLO again after STARTTLS? Are my SMTP commands correctly ordered? How can I further debug the authentication failure? Any help would be greatly appreciated! 🚀
Asked by white-wolf (11 rep)
Feb 10, 2025, 12:51 AM
Last activity: Feb 10, 2025, 12:53 AM