I was wondering if it's possible to include an attachment with sendmail. I am generating the following emailfile.eml files with the following layout

From: Company Name <example@nowhere.com>
To: recipient@madeup.com
CC: someoneelse@place.com
Subject: Generated Output

Mime-Version: 1.0

This will be the body copy even though it's terrible

I am sending these emails using

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

This part is working file but I would like to include an attachment to this email and I have not figured out how to do it

  • Use uuencode or mutt. – serenesat Aug 17 '15 at 10:11
  • I was hoping to use something more like Content-Disposition: attachment but I can't get it to work – TheLovelySausage Aug 17 '15 at 10:13
  • Do you want some text AND attachment OR attachment only? ["attachment only" case is trivial ] – AnFi Aug 17 '15 at 11:25
  • I'd like to send body copy and the attachment, preferably using sendmail and the eml file with Content-Disposition – TheLovelySausage Aug 17 '15 at 11:31
  • In such case the most simple is to use email client e.g. mutt – AnFi Aug 17 '15 at 17:39

With mutt you can simply use:

echo "This is the message body" | mutt -a "/path/to/file_to_attach" -s "subject of message" -- recipient@domain.com

Using mail command:

mail -a /opt/emailfile.eml -s "Email File" user@example.com < /dev/null

-a is used for attachments.

You can use SendEmail:

sendemail -t to@example.com -m "Here is the file." -a attachmentFile
| improve this answer | |
  • 1
    SendEmail is for Ubuntu, so not sure if it will work for CentOS or not. – serenesat Aug 17 '15 at 10:36
  • 1
    Yeah, I don't have sendemail on my workplace's CentOS, we just have sendmail. – Alexej Magura Aug 29 '17 at 18:10
  • on my ubuntu system, the mail option was -A not -a – jdex Apr 13 '19 at 21:53

Posting the solution that worked for me in case it can help anyone else, sorry it's so late.

The most reliable way I found for doing this was to include the attachment as base64 in the eml file itself, bellow is an example of the eml contents.

Note 01 : the base64 for the file comes from running the base64 command on linux using the attachment as an argument (should work with any base64 tool)

Note 02 : the string used for the boundary is just nonsense using the date and random upper case letters

Filename : emlfile.eml

From: Sender <sender@email.co.za>
To: recipient01@email.co.za
CC: recipient02@email.co.za
Disposition-Notification-To: recipient01@email.co.za
Subject: Generic Subject
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="19032019ABCDE"

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

Generic Body Copy

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

*base64 string goes here (no asterix)*


Then the filename.eml file can be sent using the command and it will include the attachment

# /usr/sbin/sendmail -t < filename.eml
| improve this answer | |
  • 2
    Great answer and completely correct. Also, for multiple attachments you can repeat the section from the second-to-last line upward to the second-to-last delimiter (inclusive) as many times as needed within the .eml file. – mttpgn Oct 22 '19 at 16:26
  • 1
    Thank you very much! <3 – TheLovelySausage Oct 23 '19 at 4:17

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.