Koozali.org: home of the SME Server

my fastest code to extract/ read an email header from an email file

Offline purvis

  • *****
  • 567
  • +0/-0
i hope this bit of code helps save somebody some time
This code does not remove carriage return characters from the header if they exist it and
   the code assumes no carriage return characters are in the header.
The returned string semailheader includes the full email header with all full lines and all lines are terminated with a linefeed.
Any lines in the original email that where wrapped in the email have been unwrapped.
All tab characters in the lines are replaced by one space character.

Code: [Select]
semailheader=$(grep -B10000 -m1 ^$ $filename | tr -s '\t' '\040'|sed -e ':a;N;$!ba;s/\n / /g')

a function for including in a bash file
Code: [Select]
function getemailheaderfromemailfile {
semailheader=$(grep -B10000 -m1 ^$ $filename | tr -s '\t' '\040'|sed -e ':a;N;$!ba;s/\n / /g')
# echo -e "$semailheader"
return
}

getemailheaderfromemailfile $filename
echo -e "$semailheader"
« Last Edit: April 10, 2013, 10:43:16 AM by purvis »

Offline Stefano

  • *
  • 10,894
  • +3/-0
Re: my fastest code to extract/ read an email header from an email file
« Reply #1 on: April 10, 2013, 11:36:02 AM »
I beg your pardon but... why?


Offline purvis

  • *****
  • 567
  • +0/-0
Re: my fastest code to extract/ read an email header from an email file
« Reply #2 on: April 10, 2013, 11:42:58 AM »
to make the above an executable here is the code for a bash file
place this code in a file named similar to "emailheader"
use nano to create the file                  nano -w -c emailheader
cut and paste the code in it
make sure there is no blank lines at the top of the file
close nano with ctrl-x
last, make the file executable  with       chmod +x emailheader

run the program with a email file name on the command line
emailheader filename
 

Code: [Select]
#!/bin/bash

filename="$*"
if  [ -f "$filename" ] &&  [ -e "$filename" ] &&  [ -r "$filename" ]
  then
  echo -e "$(grep -B10000 -m1 ^$ "$filename" | tr -s '\t' '\040'|sed -e ':a;N;$!ba;s/\n / /g')"
  exit 0
else
  exit 1
fi


Offline Stefano

  • *
  • 10,894
  • +3/-0
Re: my fastest code to extract/ read an email header from an email file
« Reply #3 on: April 10, 2013, 11:46:00 AM »
I repeat my question in another way: cui prodest? :-)

Offline purvis

  • *****
  • 567
  • +0/-0
Re: my fastest code to extract/ read an email header from an email file
« Reply #4 on: April 10, 2013, 11:48:06 AM »
Stefano
It will be used in code I will be posting later and I looked all over for a fast way to get the email headers.
This is a gift.
As far as i know, there is no built in program to get to this information in sme.
This code requires not compiling either.
 

Offline purvis

  • *****
  • 567
  • +0/-0
Re: my fastest code to extract/ read an email header from an email file
« Reply #5 on: April 10, 2013, 11:51:22 AM »
Stefano,
If this is no benefit to you. Don't use it.
And please speak English.