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?

1 Answers1

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
cas
  • 78,579
  • 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
  • Yes it is run through cron and user is for anonymity. What you're saying is completely right and was part of my initial diagnostics. The logging_script the email refers to writes two sequential dates (5 mins apart) to the log file (to ultimately determine power breaks). I can see no reason why this fairly short script (and other similar logging scripts through cron) will try to send an email (included below). From the email the "log content" is used in the body and the cron command (i.e. sudo /home/logging_script.sh) is used as "Subject text". "user" is used as sendto@email.com. – Renier Delport Mar 18 '16 at 11:40
  • My apologies. Trying to send the code mentioned, but it's a mess with the comment system. Looking at the similarities of the logging scripts - they all have a few informative echos before the log writing event. It may be that for example echo "log content" followed by echo "log content" > logfile.txt might trigger this behaviour? – Renier Delport Mar 18 '16 at 11:59
  • Also, just for in case, the log files are getting written correctly even if it tries to send the email. – Renier Delport Mar 18 '16 at 12:02
  • To answer my own question, removing the 'informative echos' before the writing event resolved the issue. Any idea why or should I try to ask this as a separate question? – Renier Delport Mar 18 '16 at 13:54
  • you don't add scripts or command output in comments. you edit your question and add the information there, and format it correctly. 2. cron always emails any job output to the owner of the crontab or the address in the MAILTO variable, unless that output is redirected to a file (it's not unusual to redirect individual crontab entries to /dev/null) - see man 5 crontab.
  • – cas Mar 18 '16 at 21:13
  • I've learnt something. Thank you! Will also edit my question in the future, thanks. There was nothing set to send email, but I've added MAILTO="" to crontab -e. I suppose it got hold of my gmail account setting after installing and setting up SSMTP. – Renier Delport Mar 19 '16 at 13:25