I am trying to include an attachment to my sendmail eml file. The current eml file (order.eml) has the following contents

From: Sender <sender@email.com>
To: client@email.co.za
Subject: Report
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="B835649000072104Jul07"

Content-Type: text/plain; charset="US-ASCII"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Body Copy

Content-Type: application/pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="por5151.pdf"
base64 por5151.pdf


both the order.eml and por5151.pdf file are in the same directory and I try to send it with

# /usr/sbin/sendmail -t < order.eml

When the email arrives I can see the por5151.pdf in the attachments but it is blank (0 bytes). I don't know why this is and I am struggling to fix it

  • Are those the actual contents? Because that attachment is 0 bytes. ;-) The eml-file must contain the data – according to the headers, a base64-encoded pdf-file – it won't parse the headers to find, encode, and include the contents itself. – The Sidhekin Aug 17 '15 at 11:13
  • Do you know how I would be able to do that? I've never added attachments before so I'm very noob at the moment – TheLovelySausage Aug 17 '15 at 11:38

What you would need to do is include the file below the

Content-Disposition: attachment; filename="por5151.pdf"

line while you generate the .eml file. You can do so using the base64 utility:

base64 por5151.pdf

Make sure the closing boundary (--B835649000072104Jul07--) gets inserted behind that.

sendmail doesn't interpret the file that you hand it, and therefore doesn't magically insert the contents of the .pdf file.

  • Hi Anthon, I made an edit to the question adding the base64 poer5151.pdf but the file seems to still be empty, did I add in the base64 correctly? – TheLovelySausage Aug 17 '15 at 11:37
  • 1
    @trent No you need to put the output of that command into .eml file while you generate it. I never assumed you would be editing this file by hand. – Anthon Aug 17 '15 at 11:42
  • 1
    Ooooooooh! I'm an idiot, I thought base64 was eml syntax I didn't realise it was a shell command! That worked perfectly thanks so much – TheLovelySausage Aug 17 '15 at 11:49

I have created below script to attach a CSV File. Its truncating the column name of CSV incorrectly and also there is one more file gets with email, namely 'ATT0001.txt'.

echo "From:"$1;
echo "To:"$2;
echo "Subject:"$3;
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 "";
echo "$4";

echo "--B835649000072104Jul07";
echo "Content-Type: text/csv";
echo "Content-Transfer-Encoding: base64";
echo "Content-Disposition: attachment; filename=\"$5\"";
base64 "$5"

echo "--B835649000072104Jul07";
) | sendmail -t

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.