1

I have a text file which is generated by some process. The file has 5 columns and 3 records as of now. The number of records can increase later.

I want the simplest way to convert those rows and columns in the tabular format and send it in the mail.

I have already checked a number of posts like below but nowhere it is meeting my exact requirement.

Send Mail HTML Table body & Attachment Text file

Send mail with an attachment and body of mail with table format

I saw various posts to implement multipart mail but i couldn't understand much. Is there a simpler way or is multipart way the only approach?

File :
Col1,col2,col3,col4,col5
Abc,0,5000,4500,True
bcd,1,9000,3000,False
DGF,3,4000,2000,True

This file I want to send in mail as a table.

3
  • What is your exact requirement? Do you need an HTML-formatted table, or will plain text be sufficient? Jan 27 '20 at 13:43
  • I want tabular format in the mail body. The table should have the columns and rows from the text file which is generated.
    – Amit
    Jan 27 '20 at 13:58
  • Please refer to my answer, if it fits your need please accept it. thanks
    – BANJOSA
    Jan 27 '20 at 17:07
1

if your source is a file with the following content

Col1,col2,col3,col4,col5
Abc,0,5000,4500,True
bcd,1,9000,3000,False
DGF,3,4000,2000,True

The below script will create create a html file with a table

#!/bin/bash
IFS=$','
first=1
echo '<table>'
while read -r cola colb colc cold colf; do
    [[ $first == 1 ]] && element=th || element=td
    first=0
    echo "<tr><$element>$cola</$element><$element>$colb</$element><$element>$colc</$element><$element>$cold</$element><$element>$colf</$element></tr>"
done < $1
echo '</table>'

To execute you need to create a .sh file and paste the script inside. Set its mode to execute

chmod +x your_script.sh

and then execute the script directing the output to a files (example: output.html)

./your_script.sh your_source_file > output.html

when its done your output should be like this

enter image description here

To send the email you can use the following

sendemail -s stmp:port -xu user -xp pass -f account_from -t destination_account -u subject -m "your_html_message" -o message-header="From: your name <account_from>" -o message-content-type=html -o message-charset=UTF-8
8
  • Thanks Banjosa, let me run that and get back to you. Thank you so much for your help.
    – Amit
    Jan 28 '20 at 9:20
  • I do not want to pass the input file to the script, rather i would like to specify the path and the file name in my script only along with the email notification logic.
    – Amit
    Jan 28 '20 at 12:51
  • so you want the location of the source file to be hardcoded in the script?
    – BANJOSA
    Jan 28 '20 at 13:48
  • yes that will be fine
    – Amit
    Jan 28 '20 at 13:56
  • just change the done < $1 to done < /path_to_your_source_file
    – BANJOSA
    Jan 28 '20 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.