l o a d i n g

Bash Script for Postfix Mail Handling

Apr 12, 2024 - Senior

$857.00 Fixed

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/[login to view URL] cat /var/log/[login to view URL] >> /var/log/[login to view URL] # Save content > /var/log/[login to view URL] # 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/[login to view URL]" LOGFILE_NON_MATCH="/var/log/[login to view URL]" 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 [login to view URL]       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 [login to view URL]                         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/[login to view URL] >> /var/log/[login to view URL] 2>&1   # Exit the script   exit 0
  • Proposal: 0
  • 69 days
AuthorImg
Girindra Nayar Inactive
,
Member since
Feb 29, 2024
Total Job
1