Bash Script for Postfix Mail Handling

Job ID: 39070995

Budget: $30 – $250 USD

I've spent to much time with Chatgpt giving me the wrong script.

Postfix will call a bash script
This script looks for a specific subject
If that subject is found, forward the email to an email address found in body.

If that subject is not found, log the entire mail to a log file,
At this time postfix is logging the email but not the header

This is my current script which is doing it wrong

#!/bin/bash


{      
  echo
    echo
  echo
  echo
  echo

} >> /var/log/turo_trips_all.log



cat /var/log/_trips.log >> /var/log/trips_all.log # Save content
> /var/log/_trips.log # empty the file

# Script to parse email and forward it based on the email found in the subject line

# Define the log files
LOGFILE_MATCH="/var/log/gmail_forward.log"
LOGFILE_NON_MATCH="/var/log/turo_trips.log"
FROM_ADDRESS="support" # Set the sender address to support
CUSTOM_SUBJECT="Please click the gmail link below to continue setting up MooveTrax Trip forwarding"
SENDER_NAME="MooveTrax" # Sender name for the forwarded email

# Define the subject pattern to match
SUBJECT_PATTERN="Gmail Forwarding Confirmation"

# Function to log messages with timestamps
log_message() {
    echo "$(date +"%Y-%m-%d %H:%M:%S") - $1" >> "$2"
  }

  # Read email from stdin (this is how Postfix pipes it to the script)
  EMAIL_CONTENT=$(cat)

  # Extract the subject from the email
  SUBJECT=$(echo "$EMAIL_CONTENT" | grep -i "^Subject:")

  # Extract the email address after "Receive Mail from" in the subject
  FORWARD_ADDRESS=$(echo "$SUBJECT" | grep -oP "(?<=Receive Mail from )[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}")

  # Extract the body of the email by stripping out the headers (everything before the first empty line)
  EMAIL_BODY=$(echo "$EMAIL_CONTENT" | sed -n '/^$/,$p' | tail -n +2)

  # Remove the first four lines of the body
  EMAIL_BODY=$(echo "$EMAIL_BODY" | tail -n +5)

  # Check if the subject matches the pattern
  if echo "$SUBJECT" | grep -q "$SUBJECT_PATTERN"; then
      # Log the entire email (including the original subject and body) to gmail_forward.log
      echo -e "Subject: $SUBJECT\n\n$EMAIL_BODY" >> "$LOGFILE_MATCH"
        log_message "Matching subject found: $SUBJECT. Forwarding email." "$LOGFILE_MATCH"

        # Forward the email if a valid address is found
          if [[ "$FORWARD_ADDRESS" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
              echo "$EMAIL_BODY" | mail -r "$FROM_ADDRESS" -s "$CUSTOM_SUBJECT" -a "From: $SENDER_NAME <$FROM_ADDRESS>" "$FORWARD_ADDRESS"
                log_message "Email forwarded to $FORWARD_ADDRESS from $SENDER_NAME <$FROM_ADDRESS>." "$LOGFILE_MATCH"
                else
                      log_message "No valid email address found in the subject. Email not forwarded." "$LOGFILE_MATCH"
                      fi
                  else
                      # Log the entire email (including the original subject and body) to turo_trips.log
                        echo -e "Subject: $SUBJECT\n\n$EMAIL_BODY" >> "$LOGFILE_NON_MATCH"
                        log_message "Non-matching subject: $SUBJECT. Email logged." "$LOGFILE_NON_MATCH"
  fi


sudo -u postfix /var/www/parse-mail.sh >> /var/log/_trips.log 2>&1

  # Exit the script
  exit 0
Related categories: Shell Script Bash Scripting