I'm trying to send an email from a Bash script, but I'm facing issues with the formatting. When I embed the output of my script directly into the email, it loses all the spacing and layout, which makes it hard to read. I'd appreciate any tips or fixes to ensure that it retains the original formatting as seen when executing the script. Here's a snippet of my script that I'm using:
```bash
#!/usr/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DATADIR=/mnt/data
HOSTNAME=$(hostname)
EMAILRECIP="[email protected]"
/usr/sbin/sendmail -it << EOF
From: Server
To: $EMAILRECIP
Subject: Quota report from $HOSTNAME
Content-Type: text/plain; charset=UTF-8
$(date)
$(echo " Path Hard-limit Soft-limit Used Available Soft-limit exceeded? Hard-limit exceeded?")
$(echo "-------------------------------------------------------------------------------------------------------------------------------")
$(ls -1 $DATADIR | while read -r DIR; do
gluster volume quota data list /"$DIR" | tail -n +3 | cut -c2-
done)
$(echo "----------------------------------------------------------------")
EOF
```
Thanks in advance!
2 Answers
Another thing to try is verifying if the `Content-Type` is set correctly; sometimes Gmail defaults to a different encoding which can mess with how the email displays. For plain text emails, you might not need to specify it if the default works fine. Give it a shot!
You can simplify your script by avoiding `$(echo ...)` when using heredocs. Here’s a revised version:
```bash
#!/usr/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DATADIR=/mnt/data
HOSTNAME=$(hostname)
EMAILRECIP="[email protected]"
/usr/sbin/sendmail -it << EOF
From: Server
To: $EMAILRECIP
Subject: Quota report from $HOSTNAME
Content-Type: text/plain; charset=UTF-8
$(date)
Path Hard-limit Soft-limit Used Available Soft-limit exceeded? Hard-limit exceeded?
-------------------------------------------------------------------------------------------------------------------------------
$(ls -1 $DATADIR | while read -r DIR; do
gluster volume quota data list /"$DIR" | tail -n +3 | cut -c2-
done)
----------------------------------------------------------------
EOF
```
This way, you should keep the formatting intact without additional processing.
Also, I noticed that when I checked the email in Gmail using 'show original', it appears to be arriving as Base64 encoded. Could that be affecting the formatting?
Thanks for that suggestion! But unfortunately, it didn't fix the issue for me. I'll keep trying different approaches.
I appreciate the tip! I’ll check that out and see if removing it helps.