Skip to content

Commit

Permalink
Remove logging to file feature and move messages to console output in…
Browse files Browse the repository at this point in the history
…stead.
  • Loading branch information
nsouto committed Aug 16, 2024
1 parent 623a75b commit b23d951
Showing 1 changed file with 12 additions and 28 deletions.
40 changes: 12 additions & 28 deletions postfix/usr/local/bin/mailparse.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,9 @@ KINESIS_STREAM_NAME="${KINESIS_STREAM_NAME:-}"
KINESIS_REGION="${KINESIS_REGION:-}"
WEBHOOK_URL="${WEBHOOK_URL:-}"
FORWARD_EMAIL="${FORWARD_EMAIL:-}"
LOG_FILE="${LOG_FILE:-/var/log/email_pipe.log}"
LOG_MAX_SIZE="${LOG_MAX_SIZE:-1048576}" # 1MB

# Function to rotate log if it exceeds the maximum size
rotate_log() {
if [ -f "$LOG_FILE" ]; then
log_size=$(stat --format="%s" "$LOG_FILE")
if [ "$log_size" -ge "$LOG_MAX_SIZE" ]; then
mv "$LOG_FILE" "$LOG_FILE.old"
echo "Log rotated at $(date)" > "$LOG_FILE"
fi
fi
}

# Rotate the log file if necessary
rotate_log

# Log the incoming email for debugging purposes
echo "Email received at $(date)" >> "$LOG_FILE"
echo "Email received at $(date)"

# Save the email content to a temporary file
TEMP_EMAIL_FILE=$(mktemp /tmp/email_XXXXXX.eml)
Expand All @@ -36,7 +20,7 @@ subject=$(grep -i "^Subject:" "$TEMP_EMAIL_FILE" | sed 's/Subject: //')
from=$(grep -i "^From:" "$TEMP_EMAIL_FILE" | sed 's/From: //')

# Log the extracted information (optional)
echo "Received an email from: $from with subject: $subject" >> "$LOG_FILE"
echo "Received an email from: $from with subject: $subject"

# Read the entire email content
email_content=$(cat "$TEMP_EMAIL_FILE")
Expand All @@ -47,7 +31,7 @@ rm -f "$TEMP_EMAIL_FILE"
# Function to send email to Kinesis
send_to_kinesis() {
if ! command -v aws &> /dev/null; then
echo "AWS CLI not found. Cannot send email to Kinesis." >> "$LOG_FILE"
echo "AWS CLI not found. Cannot send email to Kinesis."
return 1
fi

Expand All @@ -58,18 +42,18 @@ send_to_kinesis() {
--region "$KINESIS_REGION"

if [ $? -eq 0 ]; then
echo "Email successfully sent to Kinesis stream." >> "$LOG_FILE"
echo "Email successfully sent to Kinesis stream."
return 0
else
echo "Failed to send email to Kinesis stream." >> "$LOG_FILE"
echo "Failed to send email to Kinesis stream."
return 1
fi
}

# Function to send email to a webhook
send_to_webhook() {
if ! command -v curl &> /dev/null; then
echo "cURL not found. Cannot send email to webhook." >> "$LOG_FILE"
echo "cURL not found. Cannot send email to webhook."
return 1
fi

Expand All @@ -78,28 +62,28 @@ send_to_webhook() {
--data-binary "$email_content"

if [ $? -eq 0 ]; then
echo "Email successfully sent to webhook." >> "$LOG_FILE"
echo "Email successfully sent to webhook."
return 0
else
echo "Failed to send email to webhook." >> "$LOG_FILE"
echo "Failed to send email to webhook."
return 1
fi
}

# Function to forward email to another address
forward_email() {
if ! command -v sendmail &> /dev/null; then
echo "Sendmail not found. Cannot forward email." >> "$LOG_FILE"
echo "Sendmail not found. Cannot forward email."
return 1
fi

echo "$email_content" | sendmail -t "$FORWARD_EMAIL"

if [ $? -eq 0 ]; then
echo "Email successfully forwarded to $FORWARD_EMAIL." >> "$LOG_FILE"
echo "Email successfully forwarded to $FORWARD_EMAIL."
return 0
else
echo "Failed to forward email to $FORWARD_EMAIL." >> "$LOG_FILE"
echo "Failed to forward email to $FORWARD_EMAIL."
return 1
fi
}
Expand All @@ -125,7 +109,7 @@ fi

# Log if no actions were performed
if [ "$result" -eq 0 ] && [ -z "$KINESIS_STREAM_NAME" ] && [ -z "$WEBHOOK_URL" ] && [ -z "$FORWARD_EMAIL" ]; then
echo "No valid configuration found. Email content logged and discarded." >> "$LOG_FILE"
echo "No valid configuration found. Email content logged and discarded."
fi

# Exit with the result status
Expand Down

0 comments on commit b23d951

Please sign in to comment.