0

After installing ssmtp and mailutils, writing to a file with 'echo' tries to send me an email to username@hostname. For example:

Sending mail with mailutils:

echo "Body text here." | mail -s "Subject text here." sendto@email.com

and I use: echo "log content" > logfile.txt to write content to a log file.

The problem is that when I want to wite to a log file, I get an deliverable email from Gmail saying username@hostname is unreachable, meaning that it interferes with mailutils.

I this a known issue with mailutils which needs a workaround or fix and how can I approach it?

0

Are you doing something like this?

echo "log content" > logfile.txt | mail -s "Subject text" sendto@email.com

If so, it's no wonder that it won't work - you're already redirecting echo's output to a file, you can't also pipe it to mail without using a program like tee.

tee's entire purpose is to (from the man page):

tee - read from standard input and write to standard output and files

Note: if you want to append to logfile.txt rather than overwrite it completely, use tee -a logfile.txt. See man tee.

So, to save to a logfile AND pipe into mail, try this:

echo "log content" | tee logfile.txt | mail -s "Subject text" sendto@email.com

Alternatively, you can redirect to a logfile and then use < to redirect mail's stdin to be the logfile, like so:

echo "log content" > logfile.txt
mail -s "Subject text" sendto@email.com < logfile.txt
  • I'm not. The two commands mentioned above are used in completely different instances. In the meantime I've also realised that it isn't mailutils causing the clash as I've uninstalled it and I still get the same problem when running echo "log content" > logfile.txt. – Renier Delport Mar 17 '16 at 10:17
  • do you have some process monitoring that logfile and sending mail when it gets updated? or a shell script or function or alias called echo that overrides the built-in echo or /bin/echo? because neither of them send any email - they do not even have the capability to do so...that is not their job, their job is to echo their command line arguments to stdout, and nothing more. – cas Mar 17 '16 at 11:33
  • No processes monitoring the logfiles. That was the original idea, but I started to use SMSs instead. I've never worked with aliases. Is there a way to check whether I accidentally could have done so? This is my email 'X-Google-Original-From: user@hostname Received: by hostname (sSMTP sendmail emulation); Thu, 17 Mar 2016 16:08:03 +0200 Date:... To: user Subject: Cron <user@hostname> sudo /home/Scripts/logging_script.sh Content-Type: text/plain; charset=UTF-8 X-Cron-Env: <SHELL=/bin/sh> X-Cron-Env: <HOME=/home/> X-Cron-Env: <PATH=/usr/bin:/bin> X-Cron-Env: <LOGNAME=user>' – Renier Delport Mar 17 '16 at 14:18
  • Can base_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" somehow interfere? for example if echo "log content" > $base_dir/logfile.txt? – Renier Delport Mar 17 '16 at 14:29
  • you should have included that email in the original question, it's essential information for diagnosis (whereas the speculations about echo and mailutils were just irrelevant distractions). It looks like you have a cron job sending the email. BTW, is user@hostname literal or have you edited it for anonymity? Check "user"'s crontab, and the script mentioned in the error message - /home/Scripts/logging_script.sh. as for your base_dir question, no. – cas Mar 17 '16 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.