How do I preserve new line characters for sendmail in bash script?

bash, echo, sendmail

Solution

I ran into this recently. I was able to resolve it adding the '-e' option to echo.

Change this:

echo $mail | /usr/sbin/sendmail "email2@example.com"

To This:

echo -e "$mail" | /usr/sbin/sendmail "email2@example.com"

Hopefully that helps.

Problem

Why isn't the following preserving the new line characters in the resulted email? ``` #!/bin/bash file="/tmp/ip.txt" address=$(curl -s http://ipecho.net/plain; echo) ifconfig=$(ifconfig) function build_body { echo "----------------------------------------------------------------" > $file echo "IP Address: $address (according to http://ipecho.net/plain)" >> $file echo "----------------------------------------------------------------" >> $file echo >> $file echo "Result from ifconfig:" >> $file echo >> $file echo "$ifconfig" >> $file echo >> $file } build_body msg=$(cat $file) mail="subject:Home Server Status\nfrom:email@example.com\n$msg" echo $mail | /usr/sbin/sendmail "email2@example.com" ``` I receive the email this script generates, however, the whole body is all on one line! /tmp/ip.txt is exactly how I want the email to look.

Original source