简体   繁体   中英

How to Send formatted mail body Unix Script

I would like to format the mail content before I send, Below is my code,

echo "Time: `$TIMESTAMP_CMD\n\n$EMAIL_BODY" | mail -s "$EMAIL_SUBJECT" ${EMAIL_DIST}

TIMESTAMP_CMD , EMAIL_BODY , EMAIL_SUBJECT and EMAIL_DIST are variables which are defined above this code.

Here, I need line breaks between TIMESTAMP_CMD and EMAIL_BODY. How to achieve it?

Use

printf "Time: $TIMESTAMP_CMD\n\n$EMAIL_BODY" 

Also you use a back-tick in the echo which invokes shell . But it seems $TIMESTAMP_CMD holds a shell command. So you'd want something like this,

printf "Time: `$TIMESTAMP_CMD`\n\n$EMAIL_BODY" 
# or
printf "Time: $($TIMESTAMP_CMD)\n\n$EMAIL_BODY" 
# or
printf "Time: %s\n\n$EMAIL_BODY" "$($TIMESTAMP_CMD)"

If you choose to use echo then you can make use of -e switch to enable interpretation of backslash escapes something like echo -e "Time: $TIMESTAMP_CMD\\n\\n$EMAIL_BODY" . You can refer the man pages or this link for more info. And as pointed out previously please get rid of the backtick (`) when echo ing.
Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM