Search code examples
linuxunixbase64sendmailuuencode

Sendmail sending corrupted/unreadable pdf over mail, sending with base64 encoding in Linux


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: [email protected]";
echo "Cc: [email protected]";
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.


Solution

  • 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: [email protected]";
    echo "Cc: [email protected]";
    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 :-)