Can sendmail include an attachment?

104,352

Solution 1

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 <[email protected]>
To: [email protected]
CC: [email protected]
Disposition-Notification-To: [email protected]
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

Solution 2

With mutt you can simply use:

echo "This is the message body" | mutt -a "/path/to/file_to_attach" -s "subject of message" -- [email protected]

Using mail command:

mail -a /opt/emailfile.eml -s "Email File" [email protected] < /dev/null

-a is used for attachments.

You can use SendEmail:

sendemail -t [email protected] -m "Here is the file." -a attachmentFile
Share:
104,352

Related videos on Youtube

waldemar_enns
Author by

waldemar_enns

Primarily Informix-4GL Programmer, Linux Bash Programmer and Linux Administrator. Dabbler in Java, Python, HTML, PHP and JavaScript. Expert at breaking everything.

Updated on September 18, 2022

Comments

  • waldemar_enns
    waldemar_enns almost 2 years

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

    From: Company Name <[email protected]>
    To: [email protected]
    CC: [email protected]
    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.

    • serenesat
      serenesat almost 9 years
      Use uuencode or mutt.
    • waldemar_enns
      waldemar_enns almost 9 years
      I was hoping to use something more like Content-Disposition: attachment but I can't get it to work
    • AnFi
      AnFi almost 9 years
      Do you want some text AND attachment OR attachment only? ["attachment only" case is trivial ]
    • waldemar_enns
      waldemar_enns almost 9 years
      I'd like to send body copy and the attachment, preferably using sendmail and the eml file with Content-Disposition
    • AnFi
      AnFi almost 9 years
      In such case the most simple is to use email client e.g. mutt
    • Kusalananda
      Kusalananda about 8 years
      You may have a mailx implementation that supports attachments with -A...
    • JdeBP
      JdeBP over 5 years
      Answers go in answers, not in questions.
    • waldemar_enns
      waldemar_enns over 5 years
      JdeBP is right one sec, I will correct it
    • waldemar_enns
      waldemar_enns over 5 years
      There all fixed
  • Alexej Magura
    Alexej Magura almost 7 years
    Yeah, I don't have sendemail on my workplace's CentOS, we just have sendmail.
  • jdex
    jdex about 5 years
    on my ubuntu system, the mail option was -A not -a
  • mttpgn
    mttpgn over 4 years
    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.
  • waldemar_enns
    waldemar_enns over 4 years
    Thank you very much! <3
  • Simon Rose
    Simon Rose over 3 years
    This is exactly what I needed, thanks!
  • waldemar_enns
    waldemar_enns over 3 years
    You're very welcome!
  • Nikkoli
    Nikkoli about 3 years
    Thank you! This is exactly what I was looking for to send a report of daily server activities 🙏
  • k4ppa
    k4ppa about 2 years
    An example of the bash code would be useful. I tried and all I have is an email with a base64 encoded string inside the body.