21

Is it possible to include an attachment with sendmail? I am generating the following emailfile.eml files with the following layout

From: Company Name <mycompany@example.com>
To: recipient@example.com
CC: someoneelse@example.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.

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

2 Answers 2

16

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
4
  • 1
    SendEmail is for Ubuntu, so not sure if it will work for CentOS or not.
    – serenesat
    Aug 17, 2015 at 10:36
  • 1
    Yeah, I don't have sendemail on my workplace's CentOS, we just have sendmail. Aug 29, 2017 at 18:10
  • on my ubuntu system, the mail option was -A not -a
    – jdex
    Apr 13, 2019 at 21:53
  • Does it allow to specify a custom From address?
    – golimar
    Nov 24, 2020 at 8:56
14

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
7
  • 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, 2019 at 16:26
  • 1
    Thank you very much! <3 Oct 23, 2019 at 4:17
  • 1
    This is exactly what I needed, thanks!
    – Simon Rose
    Dec 11, 2020 at 8:56
  • You're very welcome! Dec 11, 2020 at 15:34
  • How do you put bash commands in eml file? Like echo $something
    – Zafar
    Jan 29, 2021 at 20:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.