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

I need to send a mail with an attachment and body of mail in table format.I have used the below code to send mail. but unable to attache file.

 (echo "From: ";    
  echo "Subject: testing of table using awk";  
  echo "Content-type: text/html";    
  echo;     
  awk 'BEGIN{print "<table>"} {print "<tr><tr>";for(i=1;i<=NF;i++)print "<td><td>" $i"</td></td>";print  "</tr></tr>"} END{print "</table>"}' input1.txt;
 ) | sendmail <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfa7a7a79fa6a6a6f1bcb0b2">[email protected]</a>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try to use a version of mail command supporting -a (attachment) switch . It’s the more reliable solution.

s-nail have it !

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2d7dfd3dbde8fd4ddddf2d0d3c1d7">[email protected]</a>

mail -v -s subject <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f2920200f2d2e3d">[email protected]</a> -a file.txt -q - <<EOF 
$(awk '
    BEGIN{print "<table>"}
    {
        print "<tr><tr>";
        for(i=1;i<=NF;i++) {
            print "<td><td>" $i"</td></td>";
            print  "</tr></tr>";
        }
    }
    END{print "</table>"}' input1.txt
)

.
EOF

Check man mail if you need to add headers and such

Method 2

Using sendmail:

TO_ADDRESS="<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b0a09082b0e130a061b070e45080406">[email protected]</a>"
SUBJECT="Test Mail"

cat << --OEF--
Subject: $SUBJECT
TO: $TO_ADDRESS
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="MAIL_BOUNDARY"


--MAIL_BOUNDARY
Content-Type: multipart/alternative;
boundary="MAIL_BOUNDARY2"

--MAIL_BOUNDARY2
Content-Type: text/plain; charset=utf-8

$SUBJECT

--MAIL_BOUNDARY2
Content-Type: text/html; charset=utf-8

--OEF--
cat MailBody.html 

cat << --OEF--

--MAIL_BOUNDARY2--

--MAIL_BOUNDARY
Content-Type: application/zip; name=file.zip
Content-Disposition: attachment; filename=file.zip
Content-Transfer-Encoding: base64

--OEF--
base64  /opt/file.zip
cat << --OEF--

--MAIL_BOUNDARY--
--OEF--
) |  /usr/sbin/sendmail $TO_ADDRESS


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x