#!/bin/bash # test_migration_email.sh - Places a test email into S3 + SQS # # Simulates the complete SES inbound flow: Mail goes to S3, metadata to SQS. # The worker picks it up and processes it (Delivery or Forward). # # Usage: # ./test_migration_email.sh cielectrical.com carlosr@cielectrical.com # ./test_migration_email.sh buddelectric.net service@buddelectric.net # # Optional sender address: # ./test_migration_email.sh cielectrical.com carlosr@cielectrical.com sender@example.com set -e # --- Parameters --- DOMAIN="$1" RECIPIENT="$2" FROM_ADDR="${3:-migration-test@andreasknuth.de}" AWS_REGION=${AWS_REGION:-"us-east-2"} if [ -z "$DOMAIN" ] || [ -z "$RECIPIENT" ]; then echo "Usage: $0 [from-address]" echo "Example: $0 cielectrical.com carlosr@cielectrical.com" exit 1 fi # --- Derived variables --- BUCKET_NAME=$(echo "$DOMAIN" | tr '.' '-')"-emails" QUEUE_NAME=$(echo "$DOMAIN" | tr '.' '-')"-queue" MESSAGE_ID="test-migration-$(date +%s)-$$" TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ") DATE_RFC2822=$(date -R) echo "============================================================" echo " Migration Test Email" echo "============================================================" echo " Domain: $DOMAIN" echo " Recipient: $RECIPIENT" echo " Sender: $FROM_ADDR" echo " Bucket: $BUCKET_NAME" echo " Queue: $QUEUE_NAME" echo " Key: $MESSAGE_ID" echo "" # --- Step 1: Create RFC822 email --- echo "[1/3] Creating test email..." TMP_FILE=$(mktemp /tmp/test-mail-XXXXXX.eml) cat > "$TMP_FILE" << EOF From: Migration Test <${FROM_ADDR}> To: ${RECIPIENT} Subject: Migration Test $(date '+%Y-%m-%d %H:%M:%S') Date: ${DATE_RFC2822} Message-ID: <${MESSAGE_ID}@test.email-srvr.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hello! This is a test email to validate the email migration pipeline. Sent: $(date) Domain: ${DOMAIN} Recipient: ${RECIPIENT} Message-ID: ${MESSAGE_ID} If you see this email in your inbox, the complete path is working: S3 -> SQS -> Worker -> Forward/Delivery -- Bay Area Affiliates - Migration Test EOF echo " Done ($(wc -c < "$TMP_FILE") bytes)" # --- Step 2: Upload to S3 --- echo "[2/3] Uploading to S3: s3://${BUCKET_NAME}/${MESSAGE_ID} ..." aws s3 cp "$TMP_FILE" "s3://${BUCKET_NAME}/${MESSAGE_ID}" \ --region "$AWS_REGION" \ --quiet echo " Done" # --- Step 3: Place SQS message in fake-SNS format --- echo "[3/3] Placing message in SQS queue..." QUEUE_URL=$(aws sqs get-queue-url \ --queue-name "$QUEUE_NAME" \ --region "$AWS_REGION" \ --output text \ --query 'QueueUrl') if [ -z "$QUEUE_URL" ]; then echo " ERROR: Queue $QUEUE_NAME not found!" rm -f "$TMP_FILE" exit 1 fi # SES event payload (what the Lambda normally produces) SES_DATA=$(jq -n \ --arg msgId "$MESSAGE_ID" \ --arg source "$FROM_ADDR" \ --arg recipient "$RECIPIENT" \ --arg ts "$TIMESTAMP" \ '{ mail: { messageId: $msgId, source: $source, timestamp: $ts, destination: [$recipient] }, receipt: { recipients: [$recipient], timestamp: $ts, action: { type: "S3", bucketName: "test", objectKey: $msgId } } }') # Fake SNS wrapper (same format as ses_sns_shim_global.py) SQS_BODY=$(jq -n \ --arg sesData "$SES_DATA" \ --arg ts "$TIMESTAMP" \ '{ Type: "Notification", MessageId: "test-\(now | tostring)", TopicArn: "arn:aws:sns:ses-shim:global-topic", Subject: "Amazon SES Email Receipt Notification", Message: $sesData, Timestamp: $ts }') SQS_MSG_ID=$(aws sqs send-message \ --queue-url "$QUEUE_URL" \ --region "$AWS_REGION" \ --message-body "$SQS_BODY" \ --output text \ --query 'MessageId') echo " Done (SQS MessageId: ${SQS_MSG_ID})" # --- Cleanup --- rm -f "$TMP_FILE" echo "" echo "============================================================" echo " Test email placed successfully!" echo "============================================================" echo "" echo " Watch worker logs:" echo " docker logs -f email-worker --tail 50" echo "" echo " Expected output:" echo " Processing: ${MESSAGE_ID:0:20}... -> ${RECIPIENT}" echo " Forwarded via legacy SMTP ... (if forward rule exists)" echo " OR" echo " Delivered to ${RECIPIENT} (if DMS mailbox exists)" echo "" echo " Check S3 object:" echo " aws s3 ls s3://${BUCKET_NAME}/${MESSAGE_ID} --region ${AWS_REGION}" echo "============================================================"