7

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
9

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
  • SendEmail is for Ubuntu, so not sure if it will work for CentOS or not. – serenesat Aug 17 '15 at 10:36
  • 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 at 21:53
0

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"

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

Generic Body Copy

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

*base64 string goes here (no asterix)*

--19032019ABCDE--

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

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

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.