Skip to content

Commit

Permalink
Refactor mailparse.sh to load environment variables if they exist, re…
Browse files Browse the repository at this point in the history
…direct stdout and stderr to Docker logs, and remove log rotation functionality.
  • Loading branch information
nsouto committed Aug 26, 2024
1 parent a13cd2e commit 9072f1e
Showing 1 changed file with 18 additions and 37 deletions.
55 changes: 18 additions & 37 deletions postfix/usr/local/bin/mailparse.sh
Original file line number Diff line number Diff line change
@@ -1,42 +1,23 @@
#!/bin/bash

# Load environment variables if .mailparse.env exists
if [ -f /usr/local/bin/.mailparse.env ]; then
. /usr/local/bin/.mailparse.env
fi

exec 2>>/var/log/email_pipe_error.log
# Redirect stdout and stderr to Docker logs
exec > >(tee -a /proc/1/fd/1)
exec 2> >(tee -a /proc/1/fd/2)

# Configuration from environment variables
KINESIS_STREAM_NAME="${KINESIS_STREAM_NAME:-}"
KINESIS_REGION="${KINESIS_REGION:-}"
WEBHOOK_URL="${WEBHOOK_URL:-}"
FORWARD_EMAIL="${FORWARD_EMAIL:-}"
DOMAIN_FILTER="${DOMAIN_FILTER:-msg.domaineasy.com}"
LOG_FILE="${LOG_FILE:-/var/log/email_pipe.log}"
LOG_MAX_SIZE=${LOG_MAX_SIZE:-1048576} # 1MB
LOG_ROTATION_COUNT=${LOG_ROTATION_COUNT:-4}

# 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
for i in $(seq $LOG_ROTATION_COUNT -1 1); do
if [ -f "$LOG_FILE.$i" ]; then
mv "$LOG_FILE.$i" "$LOG_FILE.$((i+1))"
fi
done
mv "$LOG_FILE" "$LOG_FILE.1"
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 @@ -49,12 +30,12 @@ subject=$(grep -i "^Subject:" "$TEMP_EMAIL_FILE" | sed 's/Subject: //')
from=$(grep -i "^From:" "$TEMP_EMAIL_FILE" | sed 's/From: //')
to=$(grep -i "^To:" <<< "$email_content" | sed 's/To: //')

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

# Check if the 'To' field matches the specified domain
if [ -n "$DOMAIN_FILTER" ] && ! grep -iq "^To:.*@$DOMAIN_FILTER" "$TEMP_EMAIL_FILE"; then
echo "Email does not match the 'To' domain '$DOMAIN_FILTER'. Ignoring." >> "$LOG_FILE"
echo "Email does not match the 'To' domain '$DOMAIN_FILTER'. Ignoring."
rm -f "$TEMP_EMAIL_FILE"
exit 0
fi
Expand All @@ -70,7 +51,7 @@ send_to_kinesis() {
. /root/venv/bin/activate;

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 Down Expand Up @@ -98,18 +79,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 @@ -118,28 +99,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 @@ -165,7 +146,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 9072f1e

Please sign in to comment.