Question

hey guys I've been trying to wrap my head around this for a couple days and am just losing it here, I am trying to journal a message, which (to us) means we take a typical responsive email, with your typical to / from and other special header, wrap THAT (as a file attachment) inside of ANOTHER email, which we send off to our DB.

The heart of my question is then, how would I wrap an email (with headers) inside of another email? Would I be required to write out the initial (inner) email to file first and add that through MUTT as an attachment? Could I write the attachment without creating a file? I have a stub of how I think it should work?

#!/bin/bash
function assemble()
{
    declare -a argAry=("${!1}")
    echo -e "${argAry[@]}"  -- $2 |sed "s/^ *//;s/ *$//;s/ \{1,\}/ /g"
}

function generate()
{
  hname=`hostname`
  tai64=`date| tai64n |cut -c2-25`
  uuid4=`python  -c 'import uuid; print uuid.uuid4()'`

  # return variable as generated string
  echo "${hname}-${tai64}-${uuid4}'"
}

function send()
{

  attach='' # can't [] && || for some reason
  [ '1' == ${ARGS[0]} ] && attach="-e 'my_hdr"

  local hdrsTable=(
    "${attach} To:                     ${ARGS[1]}'\n"
    "${attach} From:                   ${ARGS[2]}'\n"

    "${attach} X-DOMAIN-SITE-URL:      ${ARGS[3]}'\n"
    "${attach} X-DOMAIN-MEDIA-TYPE:    ${ARGS[4]}\n"
    "${attach} X-DOMAIN-DIRECTION:     ${ARGS[5]}\n"
    "${attach} X-DOMAIN-CAPTURE-DATE:  `date`\n"
    "${attach} X-DOMAIN-POST-DATE:     `date`\n"
    "${attach} X-DOMAIN-UTID:          `generate`\n"
    `[ -a $FILE ] && echo -a $FILE`
  ) 

  if [ '0' == $ARGV[0] ]; then
    FILE="/tmp/$(basename $0).$$.tmp"
    assemble hdrsTable[@] >> "/tmp/${FILE}" # create the temporary file to hold stuff?
  else
    assemble hdrsTable[@] $1
  fi
}

ARGS=("0" "somelivename@domain.com" "someliveemail@domain.com" "www.google.com" "archiver" "out")
send 

ARGS=("0" "someforwarder@domain.com" "journaling@ash.domain.com" "www.google.com" "archiver" "out")
send 'root@domain.com'
Was it helpful?

Solution

Basically, you have to construct a "multipart/mixed" email message with a "message/rfc822" part that holds the email you want to enclose.

inner_msg=$(cat - <<END_MSG
To: santa.clause@northpole.example.com
From: virginia@doubtful.invalid
Subject: do you exist

I am doubtful

Regards,
V.
END_MSG
)

Then you can build the message to send:

boundary="this is the boundary: $$-$(date +%s)"

msg=$(cat - << END_MSG
To: recipient@example.com
From: sender@example.com
Subject: I contain a message
Content-Type: multipart/mixed; boundary="$boundary"
Mime-Version: 1.0

This is a multipart message in MIME format.

--$boundary
Content-Type: text/plain; charset=ISO-8859-1

This is the body of the 'container' message.
The email attachment follows.

--$boundary
Content-Type message/rfc822
Content-Disposition: attachment; filename="attached_email.eml"

$inner_msg

--$boundary--
END_MSG
)

You can now send "$msg" using whatever mechanism you choose:

echo "$msg" | sendmail -oi -t

(this is untested and may be buggy)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top