1

I'm using msmtp to create e-mail alerts on some RPi kiosks we have set up at my company, and unfortunately if I try to include the last 5 lines of a log file in the e-mail alert, msmtp doesn't handle the line breaks correctly and it prints all on a single line of text. If \n is included in each line in the log file, it outputs correctly in the e-mail alert.

A basic example for this command would be:

echo -e "This is a test. $(tail -5 /var/log/syslog)" | msmtp @domain

which does echo the syslog; yet it's all in one wall of text because msmtp doesn't seem to handle line breaks. If I manually edit the syslog and add \n to the end of each line, it prints properly

What's the proper syntax for sed or awk to add \n to the end of every line in a log file?

  • Is the e-mail in html format? – Gerard H. Pille Mar 27 at 15:53
  • No; the email is simply echo piped into msmtp; example: echo "hello" | msmtp recipient@domain.com; but I'm using tail to paste the last couple lines of a log file into the e-mail body, and it all prints on one line unless the log file has "\n" at the end of each line. – Geo Mar 27 at 15:57
  • 1
    Welcome! Could you post an example? – schrodigerscatcuriosity Mar 27 at 15:59
  • Sure; a basic example for this command would be: echo -e "This is a test. $(tail -5 /var/log/syslog)" | msmtp @domain - which does echo the syslog; yet it's all in one wall of text because msmtp doesn't seem to handle line breaks. If I manually edit the syslog and add \n to the end of each line, it prints properly. – Geo Mar 27 at 16:03
  • Are you adding \n literally (i.e. a backslash and a newline), or another newline character, or something else, perhaps a carriage return (often presented as \r)? The newline is what you already have in the log file, in any unix text file at the end of each and every line, so adding another one doesn't seem like something that should be needed. – ilkkachu Mar 27 at 16:56
0

My guess is that the mail tool is expecting \r\n. This is also the line endings used on Microsoft's products. (they (or maybe the writers of CP/M) did not know how to transform it when sending it to a printer, so as a work around made every file have this line ending. msmtp may have its own reasons.).

Therefore try piping it through unix2dos.

i.e.

echo -e "This is a test. $(tail -5 /var/log/syslog)" | unix2dos | msmtp @domain

Also unless you intend echo to interpret the log. I would do

{
  echo -n "This is a test. "
  tail -5 /var/log/syslog
} |
  unix2dos | 
  msmtp 
| improve this answer | |
  • It's part of the original RFC821 specification for SMTP (later replaced by RFC5321), and nothing whatsoever to do with Microsoft. If msmtp doesn't transform \n to \r\n this could indeed be the issue though. – roaima Mar 27 at 23:36
  • DOS uses \r\n because of CP/M. It has nothing to do with MS. HTTP also uses \r\n – phuclv Mar 28 at 3:35
-1

This is very simple. The input to msmtp is supposed to be a properly formatted mail message, with headers and body. It is, after all, another drop-in replacement for /usr/{bin,lib}/sendmail. You've not delimited headers from body, or indeed generated any headers at all. Your system log lines are being treated as (some very odd) headers.

headers. newline. body. Generate that.

| improve this answer | |

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.