You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Bash通过MX服务器批量验证邮箱地址的脚本需求

Bash Script to Validate Email Recipients via MX Record Check & Telnet

Got it, let's put together a script that does exactly what you need—no fancy tricks, just straightforward steps tailored for someone new to Bash. Here's a complete, commented script that walks through each part of your request:

#!/bin/bash

# Check if input file is provided
if [ $# -ne 1 ]; then
    echo "Usage: $0 <email-list.txt>"
    exit 1
fi

EMAIL_FILE="$1"
VALID_EMAILS="valid_emails.txt"
INVALID_EMAILS="invalid_emails.txt"

# Clear previous result files (uncomment the lines below if you want to append instead)
> "$VALID_EMAILS"
> "$INVALID_EMAILS"

# Loop through each line in the email file
while IFS= read -r email; do
    # Skip empty lines
    [[ -z "$email" ]] && continue

    # Check if email follows basic user@domain format
    if ! [[ "$email" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ ]]; then
        echo "$email" >> "$INVALID_EMAILS"
        echo "Skipping invalid format: $email"
        continue
    fi

    # Extract the domain part after @
    DOMAIN=$(echo "$email" | cut -d '@' -f 2)
    echo "Processing $email (domain: $DOMAIN)"

    # Get the highest-priority MX record for the domain
    # Using dig +short for clean output, sort by priority (lowest number = highest priority)
    MX_SERVER=$(dig +short mx "$DOMAIN" | sort -n | head -n 1 | awk '{print $2}')

    # Check if MX record exists for the domain
    if [[ -z "$MX_SERVER" ]]; then
        echo "$email" >> "$INVALID_EMAILS"
        echo "No MX record found for $DOMAIN"
        continue
    fi

    # Remove trailing dot from MX server address if present
    MX_SERVER=${MX_SERVER%.}

    # Use telnet to validate recipient via SMTP commands, timeout after 10s to avoid hanging
    SMTP_RESPONSE=$(timeout 10 telnet "$MX_SERVER" 25 << EOF
HELO localhost
MAIL FROM:<test@example.com>
RCPT TO:<$email>
QUIT
EOF
    )

    # Check if the RCPT TO command returned a success code (250 = accepted)
    if echo "$SMTP_RESPONSE" | grep -q "250.*OK"; then
        echo "$email" >> "$VALID_EMAILS"
        echo "$email is valid"
    else
        echo "$email" >> "$INVALID_EMAILS"
        echo "$email is invalid or server rejected verification"
    fi

done < "$EMAIL_FILE"

echo "Validation complete! Results saved to $VALID_EMAILS and $INVALID_EMAILS"

How This Script Works (Step-by-Step)

  • Input Check: First, it makes sure you provided an email list file. Run it like ./validate_emails.sh your_emails.txt.
  • Result Files: It clears any existing valid_emails.txt and invalid_emails.txt (comment out the > "$VALID_EMAILS" lines if you want to append new results instead of overwriting old ones).
  • Email Format Check: Uses a basic regex to skip emails that don't follow the standard user@domain structure.
  • Domain Extraction: Pulls the domain part after @ using the cut command.
  • MX Record Lookup: Uses dig to fetch MX records, sorts them by priority (lowest number = highest priority), and picks the top server to test.
  • SMTP Validation: Connects to the MX server on port 25, sends basic SMTP commands, and checks if the server accepts the recipient address. The timeout command prevents the script from hanging if a server doesn't respond.
  • Result Saving: Writes valid emails to valid_emails.txt and invalid ones (including bad format/no MX records) to invalid_emails.txt.

Notes for New Bash Users

  • Make the script executable first: Run chmod +x validate_emails.sh.
  • You'll need these tools installed: dig (part of the dnsutils package), telnet, and timeout (usually pre-installed on most Linux systems).
  • Some email servers block SMTP verification from unknown IPs or require a valid sender address—you can change test@example.com to a real email if needed, but most servers accept a generic one for verification.
  • If you get "connection refused" errors, some MX servers might use port 587 instead of 25—just change the 25 in the telnet line to 587 and test.

内容的提问来源于stack exchange,提问作者Ateeb Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:22:06