[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [msmtp-users] from header domain not set when using msmtp with cron (solved)



On 02/12/2012 8:25 AM, Marcus C. Gottwald wrote:

Hello Chris,

you wrote (Sat 2012-Dec-01 18:18:47 -0400):

cat - |
while read line

Note that Bash's "read" without "-r" does some sort of backslash
escaping. I'm not sure which characters are special and what
exactly is replaced with what; but at the least, you'll get some
backslashes stripped. When processing unknown input using "read",
you'll almost always want to use "-r".

    $ echo 'a\\\b' | while read ; do echo "$REPLY" ; done
    a\b
    $ echo 'a\\b' | while read ; do echo "$REPLY" ; done
    a\b
    $ echo 'a\\b' | while read -r ; do echo "$REPLY" ; done
    a\\b
    $ echo '\\\' | while read ; do echo "$REPLY" ; done
    $


      line=`echo $line | sed 's/\(From: *[^ ]*\)/\1@...318.../'`
..
    echo $line >> $tmpfile

Note that "echo" (without "-e") simply reads all arguments and
produces them as its output. In the lines above, the contents of
the variable "line" will be split into words (by Bash) and passed
to "echo" as multiple arguments. If those words were delimited
(including e.g. leading and trailing whitespace) by something other
than the delimiter used by "echo" on its output side (which should
be the first character of the "IFS" special variable, i.e. usually
a space), the other delimiters will be lost. To avoid having Bash
split the variable value into words, make sure the contents of the
variable gets passed to "echo" as a single argument:

    $ LINE='one  two'
    $ echo $LINE
    one two
    $ echo "$LINE"
    one  two


cat $tmpfile | $msmtp_path $@

Note that $@ is different from "$@". The latter form ensures that
each argument given to your script will be passed as one argument
instead of getting treated by word-splitting:

    $ sh -c 'echo $@ ; echo "$@"' -- 'one  two' ' three  four'
    one two three four
    one  two  three  four


Your script also doesn't seem to make a difference between an
email's header and the body. And it isn't really prepared for
filenames containing whitespace. A shorter suggestion (not tested,
simply picking up your sed command) would be:

    #!/bin/sh
    #this replaces /usr/sbin/sendmail and adds domain to from address

    sed '1,/^$/ {s/\(From: *[^ ]*\)/\1@...318.../}' | /usr/bin/msmtp "$@"


I was waiting for someone to recognize my poor bash scripting abilities...

I've briefly tested your script and it seems to work well. It's certainly a much more elegant solution. Thanks for your feedback and the resulting script. I'll be sure to put it to use.


--
Chris Purves
Visit my blog: http://chris.northfolk.ca

"Just look at the world around you; tell me why is there so much need. Because of greed." - Damien Dempsey