Learner
Learner

Reputation: 21393

How to send mail in proper format

I am having a simple script that does system updates and sends mail about the status like this:

echo '===============================\n' > mylog.log
sudo apt-get update -y >> mylog.log
if [ $? -eq 0 ]; then
    echo 'completed successfully\n' >> mylog.log
else
    echo 'FAILED\n' >> mylog.log
fi
echo '===============================' >> mylog.log
sudo apt-get dist-upgrade -y >> mylog.log
if [ $? -eq 0 ]; then
    echo 'completed successfully\n' >> mylog.log
else
    echo 'FAILED\n' >> mylog.log
fi
echo 'sending mail\n' >> mylog.log 
mailx -aFrom:[email protected] -a 'Content-Type: text/html' -s "Updates @`date`" [email protected] < mylog.log

The program is working fine and I am getting mail. But the mail is not having any new lines. Here is the content of mail:

=============================== Hit http://security.ubuntu.com precise-security/multiverse Translation-en Hit http://security.ubuntu.com precise-security/restricted Translation-en Hit http://security.ubuntu.com precise-security/universe Translation-en Reading package lists... completed successfully =============================== Reading package lists... Building dependency tree... Reading state information... 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. completed successfully sending mail

The mail is displayed in single line which is not in readable format. If I open the log file mylog.log, it is in proper format with new lines.

=============================== 
Hit http://security.ubuntu.com precise-security/multiverse Translation-en 
Hit http://security.ubuntu.com precise-security/restricted Translation-en 
Hit http://security.ubuntu.com precise-security/universe Translation-en 
Reading package lists... 
completed successfully 
=============================== 
Reading package lists... 
Building dependency tree... 
Reading state information... 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. 
completed successfully 
sending mail

How can I send mail in proper readable format?

Update: Used printf in code, but same issue.

printf '%b' '===============================\r\n' > mylog.log
    sudo apt-get update -y >> mylog.log
    if [ $? -eq 0 ]; then
        printf '%b' 'completed successfully\r\n' >> mylog.log
    else
        printf '%b' 'FAILED\n' >> mylog.log
    fi
    printf '%b' '===============================' >> mylog.log
    sudo apt-get dist-upgrade -y >> mylog.log
    if [ $? -eq 0 ]; then
        printf '%b' 'completed successfully\r\n' >> mylog.log
    else
        printf '%b' 'FAILED\n' >> mylog.log
    fi
    printf '%b' 'sending mail\n' >> mylog.log 
    mailx -aFrom:[email protected] -a 'Content-Type: text/html' -s "Updates @`date`" [email protected] < mylog.log

Upvotes: 0

Views: 65

Answers (3)

tripleee
tripleee

Reputation: 189307

You are declaring the content to be HTML, but apparently what you are sending is a simple text file. Either add HTML formatting to have it display as expected, or send it as text/plain instead. (Personally, I would emphatically suggest the latter.)

Upvotes: 1

Cyrus
Cyrus

Reputation: 88553

I suggest to use:

printf '%b' 'text\r\n'

Upvotes: 1

Мона_Сах
Мона_Сах

Reputation: 322

Do

echo -e '===============================\n'

By default bash will not interpret backslashes .

Upvotes: 0

Related Questions