How to save new string characters for sendmail in bash script?

Why aren’t new string characters saved in the above letter?

#!/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 get the email that the script generates, but the whole body is on the same line! /tmp/ip.txt is exactly how I want the email to look.

+3
source share
4 answers

I came across this recently. I was able to solve it by adding the -e option for the echo.

Change this:

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

To that:

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

Hope this helps.

+8
source

" " (<<END), .

#!/bin/bash

address=$(curl -s http://ipecho.net/plain; echo)

function build_body
{
cat <<END
----------------------------------------------------------------
IP Address: $address (according to http://ipecho.net/plain) 
----------------------------------------------------------------

Result from ifconfig:

END
ifconfig
echo    
}


( cat <<END; build_body) | /usr/sbin/sendmail -i -- "email2@example.com"
Subject:Home Server Status
From:email@example.com

END
+4
  • : echo "$mail" | /usr/bin/sendmail ...
  • There should not be two \nbetween the headers and the message:

how in:

mail="subject:Home Server Status\nfrom:email@example.com\n\n$msg"
+3
source
{
    printf "subject:Home Server Status\nfrom:email@example.com\n\n"
    cat "$file"
} | /usr/sbin/sendmail "email2@example.com"

With the echo $mailcontents of the file appears on the command line, and bash processes them with a word extension. With cat "$file"the file name is displayed on the command line, but the contents of the file is not and therefore safe from bash.

+2
source

All Articles