10 November 2021
Sometimes it is required to send report via mail and it is very simple to do that via bash because it offers a very easy and flexible way to send email.
In this post I will present you three different email cases and for each of them a little scenario:
1. Only body (text/html), without attachment
2. Only attachment, no body (text/html)
3. Both body (text/html) and attachment
To following examples could be execute in dev-mailserver inside example-sender container.
This case is usefull when you need to communicate to the user a simple text and avoid him/her to open an attachment to read the content.
echo -e "Hello,\n\nThis is a test" > /tmp/mail-body.txt
export MAIL_TO="[INSERT HERE EMAILS, DIVIDED BY ',', THAT RECEIVE THE REPORT. Example: user@example.com,another-user@example.com ]"
mailx -s "Test report: case Only body (text/html), without attachment" "${MAIL_TO}" < /tmp/mail-body.txt
This case is usefull when you need to communicate to the user and attach one or more files, such as a reports in xlsx, but without a body text.
Steps:
1. Transform all attachments from unix to Dos (carriage return from LF to CRLF) and then using uuencode transform ASCII representation of files
2. Send mail
Note: for semplicity attachments in this case are all text files, but it works also for other file types.
ATTACHMENT_1=/tmp/attachment-1.txt
echo -e "Attachment 1\nFirst attachment" > /tmp/attachment-1.txt
ATTACHMENT_2=/tmp/attachment-2.txt
echo -e "Attachment 2\nSecond attachment" > /tmp/attachment-2.txt
ATTACHMENTS=/tmp/tmp-attachments
unix2dos < ${ATTACHMENT_1} | uuencode `basename ${ATTACHMENT_1}` > ${ATTACHMENTS}
unix2dos < ${ATTACHMENT_2} | uuencode `basename ${ATTACHMENT_2}` >> ${ATTACHMENTS}
export MAIL_TO="[INSERT HERE EMAILS, DIVIDED BY ',', THAT RECEIVE THE REPORT. Example: user@example.com,another-user@example.com ]"
mailx -s "Test report: Only attachments, no body (text/html)" "${MAIL_TO}" < ${ATTACHMENTS}
This case is usefull when you need to communicate to the user and attach one or more files, such as a reports in xlsx.
Steps:
1. Transform all attachments from unix to Dos (carriage return from LF to CRLF) and then using uuencode transform ASCII representation of files
2. Concatenate message and attachments
3. Send mail
Note: for semplicity attachments in this case are all text files, but it works also for other file types.
ATTACHMENT_1=/tmp/attachment-1.txt
echo -e "Attachment 1\nFirst attachment" > /tmp/attachment-1.txt
ATTACHMENT_2=/tmp/attachment-2.txt
echo -e "Attachment 2\nSecond attachment" > /tmp/attachment-2.txt
ATTACHMENTS=/tmp/tmp-attachments
unix2dos < ${ATTACHMENT_1} | uuencode `basename ${ATTACHMENT_1}` > ${ATTACHMENTS}
unix2dos < ${ATTACHMENT_2} | uuencode `basename ${ATTACHMENT_2}` >> ${ATTACHMENTS}
echo -e "Hello,\n\nThis is a test" > /tmp/mail-body.txt
BODY_MESSAGE=/tmp/tmp-body-message
cat /tmp/mail-body.txt ${ATTACHMENTS} > ${BODY_MESSAGE}
export MAIL_TO="[INSERT HERE EMAILS, DIVIDED BY ',', THAT RECEIVE THE REPORT. Example: user@example.com,another-user@example.com ]"
mailx -s "Test report: Both body (text/html) and attachment" "${MAIL_TO}" < ${BODY_MESSAGE}