0

I am using a shell script to send the email, I am using code as shown below:

declare -a ATTACH
ATTACH="TEST.pdf"
(
echo "To: user1@domain.com";
echo "Cc: user2@domain.com";
echo "Subject: Example Subject";
echo "MIME-Version: 1.0";
echo "Content-Type:multipart/mixed; boundary=\"B835649000072104Jul07\"";

echo "--B835649000072104Jul07";
echo "Content-Type: text/html;charset=\"UTF-8\"";
echo "Content-Transfer-Encoding: 7bit";
echo "Content-Disposition: inline";
echo "<html><head></head><body>Example</body></html>"
echo ""
echo "--B835649000072104Jul07";
echo 'Content-Type: application/pdf; name="'$(basename $ATTACH)'"';
echo "Content-Transfer-Encoding: base64";
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"';
base64 $(basename $ATTACH)
echo
echo "--B835649000072104Jul07";
) | /usr/sbin/sendmail -t -oi

My aim is to send HTML text in body and pdf attachment. The above code is able to send the valid CSV file with Content-type: text.csv and sending pdf files as well using the above script but that pdf file is unreadable or corrupted. I searched thoroughly about this issue but only I found another encoding uuencode encoding but I heard base64 encoding is much better and efficient.
What changes I should do to send the pdf file efficiently and completely valid. I'll appreciate any answer.

Mohammad Rijwan
  • 335
  • 3
  • 17
  • Take a look at [this](https://stackoverflow.com/questions/33470547/send-html-message-with-attachement-pdf-from-shell) question. – sunriax Aug 08 '20 at 19:18
  • Thanks, @sunriax. I missed one command, it's working now. – Mohammad Rijwan Aug 08 '20 at 20:42
  • `uuencode` is used as a generic term for encoding 8-bit binary as 7-bit ASCII. `-base64` is an optional encoding that `uuencode` may use which is slightly faster and more compact than the typical `uuencode` default. (a 2% savings in encoded file size) – David C. Rankin Aug 08 '20 at 20:49

1 Answers1

2

okay, I have done little change in code, and It worked when I gave OpenSSL command with base64.

declare -a ATTACH
ATTACH="TEST.pdf"
(
echo "To: user1@domain.com";
echo "Cc: user2@domain.com";
echo "Subject: Example Subject";
echo "MIME-Version: 1.0";
echo "Content-Type:multipart/mixed; boundary=\"B835649000072104Jul07\"";

echo "--B835649000072104Jul07";
echo "Content-Type: text/html;charset=\"UTF-8\"";
echo "Content-Transfer-Encoding: 7bit";
echo "Content-Disposition: inline";
echo "<html><head></head><body>Example</body></html>"
echo ""
echo "--B835649000072104Jul07";
echo 'Content-Type: application/pdf; name="'$(basename $ATTACH)'"';
echo "Content-Transfer-Encoding: base64";
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"';
echo
openssl base64 < /home/username/TEST.pdf
) | /usr/sbin/sendmail -t -oi

It's working now :-)

Mohammad Rijwan
  • 335
  • 3
  • 17
  • Good job for continuing the search and finding the answer. You may want to test the savings `bash64` provides against the default. Documentation says default encoding results in a `1.37 * original_size` while base64 results in `1.35 * original_size`. What "slightly faster" means is anyone's guess. – David C. Rankin Aug 08 '20 at 20:53
  • Thanks, @DavidC.Rankin :-) – Mohammad Rijwan Aug 10 '20 at 22:00